typescript · level 3 · unit 2 · lesson 2
- meet it
- guess first
- your turn
- fix it
- nailed it
Tap a green word if you get lost.
meet it
checks a value without forgetting its details.
A type annotation, like const loose: Colours = {...}, checks the value and then forgets it. From that line on, TypeScript only knows the annotation. With Record<string, string> it thinks any key might exist, so a typo like loose.purple slips through as undefined.
satisfies checks the value against a type and keeps what it learned about the value itself. Write it after the value: { red: "#f00" } satisfies Colours. Wrong values are still caught, and every exact key stays known.
type Colours = Record<string, string>;
const loose: Colours = { red: "#f00", green: "#0f0" };
const exact = { red: "#f00", green: "#0f0" } satisfies Colours;
console.log(loose.purple);
console.log(exact.red.toUpperCase()); An annotation goes before the =, while satisfies goes after the value.
guess first
Order something off the menu. What happens?
Guess before you run it. Wrong guesses are how you learn, and nothing is marked.
type Prices = Record<string, number>;
const menu = { tea: 2, cake: 3 } satisfies Prices;
console.log(menu.tea + menu.cake);
console.log(menu.coffee);
your turn
Your turn. Rate a new game.
Add one more game to the list: type its name in the first blank and a level in the second. The level must be one of the three in Level.
fix it
The theme maths has gone wrong. Fix it.
- Read the error: the checker will not multiply
theme.gap. What type does it thinkgaphas? - The annotation makes every property
string | number, so the checker forgot thatgapis a number. - Remove
: Themefrom line 2 and writesatisfies Themeafter the closing curly bracket instead.
nailed it
your values pass the check and keep their shape. :D
- An annotation checks a value, then remembers only the annotation.
satisfieschecks the value and keeps its exact keys and types.- Use
satisfiesfor settings, themes and lookup tables.
new words in your collection
- satisfies
This lesson isn’t saved. A free account remembers every lesson you finish.