satisfies
Annotations widen
A wide annotation like a Record of string to string erases literal detail. Every value becomes plain string and exact keys are forgotten.
Wide is just string, so the sky literal blue is lost. Autocomplete and typo checks suffer the same way.
const palette: Record<string, string> = { sky: "blue", leaf: "green", }; type Wide = typeof palette.sky; const wide: Wide = "any text"; console.log(wide, palette.sky);
any text blue
Check with satisfies
satisfies checks the value against a shape without widening it. The object must fit a Record of string to string, yet keeps its own narrow type.
A wrong value or a non-string still fails the check. Valid code passes with literals intact.
const theme = { sky: "blue", leaf: "green", } satisfies Record<string, string>; console.log(theme.sky, theme.leaf);
blue green
Keep literal types
Because the literal types survive, indexed access reads them back exactly. Sky is the single string blue rather than the whole string type.
Hover info and autocomplete now show real values. Refactors stay safe since misspelled keys fail at once.
const theme = { sky: "blue", leaf: "green", } satisfies Record<string, string>; type Sky = (typeof theme)["sky"]; const sky: Sky = "blue"; console.log(sky);
blue
Export exact shapes
typeof theme exports the exact shape for reuse across the codebase. Functions and props can share one source of truth.
current must match the theme keys and value literals. One definition drives them all with no drift.
const theme = { sky: "blue", leaf: "green", } satisfies Record<string, string>; type Theme = typeof theme; const current: Theme = { sky: "blue", leaf: "green", }; console.log(current.sky, current.leaf);
blue green
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does a wide annotation such as
Record<string, string>do to the values?Answer
It widens the values to plain string and forgets exact keys
The annotation becomes the type of the variable, so the narrow literals are lost.
-
What does
satisfiesdo?Answer
It checks the value against a shape without widening it
The check happens, yet the variable keeps the narrow type it was given.
-
What is
(typeof theme)["sky"]whenthemewas declared withsatisfies?Answer
"blue"satisfies keeps the literal, so the indexed access reads the exact string blue.
-
Why export
typeof themeas a type?Answer
To share one exact shape between functions and props
The derived type keeps every key and literal, and one definition keeps the code from drifting.
-
What does this program print?
const palette: Record<string, string> = { sky: "blue", leaf: "green", }; type Wide = typeof palette.sky; const wide: Wide = "any text"; console.log(wide, palette.sky);Answer
any text blue
Wide is plain string, so any text fits, while palette.sky still holds blue.
-
What does this program print?
const theme = { sky: "blue", leaf: "green", } satisfies Record<string, string>; console.log(theme.sky, theme.leaf);Answer
blue green
The check passes, and the object keeps its two values.
-
What does this program print?
const theme = { sky: "blue", leaf: "green", } satisfies Record<string, string>; type Sky = (typeof theme)["sky"]; const sky: Sky = "blue"; console.log(sky);Answer
blue
Sky is the literal type "blue", and the constant holds that text.
-
What does this program print?
const theme = { sky: "blue", leaf: "green", } satisfies Record<string, string>; type Theme = typeof theme; const current: Theme = { sky: "blue", leaf: "green", }; console.log(current.sky, current.leaf);Answer
blue green
The current object matches the exact shape and prints its two values.
-
What does the compiler say about this program?
const theme = { sky: "blue", leaf: 3, } satisfies Record<string, string>; console.log(theme);Answer
error TS2322: Type 'number' is not assignable to type 'string'.
satisfies still checks every value, and the number 3 does not fit the string values.
-
What does the compiler say about this program?
const theme = { sky: "blue", leaf: "green" } satisfies Record<string, string>; type Theme = typeof theme; const current: Theme = { sky: "blue" }; console.log(current);Answer
error TS2741: Property 'leaf' is missing in type '{ sky: string; }' but required in type '{ sky: string; leaf: string; }'.
The derived type keeps both exact keys, and the object provides only one.
Article author: Arthur Isaev