satisfies in TypeScript
Check a value against a type without widening it to that type.
type ThemeConfig = Record<string, { bg: string; dark: boolean }>;
const annotated: ThemeConfig = {
monokai: { bg: "#272822", dark: true },
};
const checked = {
monokai: { bg: "#272822", dark: true },
paper: { bg: "#ffffff", dark: false },
} satisfies ThemeConfig;
// annotated.monokai is checked, but the keys are widened to string
const anyKey: string = Object.keys(annotated)[0]!;
// checked keeps its exact keys and value types
type ThemeName = keyof typeof checked;
const name: ThemeName = "paper";
const isDark: false = checked.paper.dark;
console.log(anyKey, name, isDark);
How it works
- An annotation widens;
satisfieskeeps the literal types. - So you get both the check and the precise keys.
- It is the right tool for a config object.
Keywords and builtins used here
ObjectThemeConfigThemeNamebooleanconstfalsestringtruetype
The run, in numbers
- Lines
- 20
- Characters to type
- 584
- Tokens
- 124
- Three-star pace
- 100 tpm
At the three-star pace of 100 tokens a minute, this run takes about 74 seconds.
Step 1 of 2 in satisfies & strictness, step 21 of 23 in Types & annotations.