as vs satisfies
as tells the compiler to trust your type and check nothing, while satisfies checks that the value matches the type and keeps the exact type inferred from the value.
Side-by-side comparison
#| Aspect | Type assertion (as) | satisfies |
|---|---|---|
| Checks that the value matches the type | No | Yes |
| Type of the resulting variable | The given type (Type) | The type inferred from the value |
| A typo or a missing field | Won’t be reported | Will be reported by the compiler |
| Keeps literals and specific keys | No | Yes |
| Safety | Low, easy to fool the compiler | High, the compiler still watches the shape |
| Typical use | A DOM element, data already checked earlier | Configuration objects, color maps, routes |
Example
#type Palette = Record<string, string>
const broken = { accent: '#a78bfa' } as { accent: number }
console.log(typeof broken.accent)
const safe = { accent: '#a78bfa' } satisfies Palette
console.log(safe.accent.toUpperCase())string #A78BFA
Definition and usage
#Both syntaxes pair an expression with a type, and you write both after the value: value as Type and value satisfies Type. In both cases nothing changes in the running program, because these are compile-time mechanisms.
The difference is in what the compiler does. as checks nothing: it swaps the expression’s type for the given one, even if the value doesn’t match it at all, and the error only surfaces at runtime. satisfies checks that the value matches the type, and reports an error for a typo or a missing field, but leaves the type inferred from the value itself, not from the annotation. Thanks to that, after satisfies you still see the exact literals and keys of the object, while after as you only see the given type.
Leave as for situations where you know more than the compiler, for example a DOM element after querySelector. Where you want the shape of a configuration object, a color map, or a set of routes checked, while also keeping precise value types, choose satisfies.
Syntax
#const a = expression as Type
const b = expression satisfies TypeMore examples
#type Config = { fontSize: number }
const wrong = { fontsize: 16 } as Config
console.log(wrong.fontSize)undefined
The typo fontsize instead of fontSize wasn’t reported, because as only overwrites the type. In the running program, the field fontSize simply doesn’t exist.
type Config = { fontSize: number }
const correct = { fontsize: 16 } satisfies Config
// Error: Object literal may only specify known properties, and 'fontsize' does not exist in type 'Config'.Good practices
#- Treat
asas a last resort, especially with data from outside. If you can, check the shape with a type guard instead. - After
satisfies, the variable has the type inferred from the value, not from the annotation, so the editor’s suggestions show the exact keys and literals. - A double assertion
as unknown as Tis a sign that something is off with the types.satisfiesnever requires a workaround like that.
Related entries
#- Type assertion (as)Telling the compiler the type of a value when you know more than it does.
- satisfiesChecks that a value matches a type without changing the inferred type.
- as constFreezes literals in an expression: the narrowest types and read-only fields.
- unknownThe safe counterpart to any: a value of an unknown type that you must check before use.
Spotted a mistake or missing an example? Write to us.