type widening
The compiler giving a value a broader type than its exact value, such as string instead of "production", because it might change later.
In more detail
let variables and object properties are widened because they can be reassigned. Widening loses the exact values you may want to check later. as const, const type parameters and satisfies are ways to keep the narrow literal types.
Related terms
- literal type — A type that allows exactly one value, such as the string "GET", the number 404 or true.
- as const — The TypeScript suffix as const, which tells the compiler to treat an object's values as fixed literal values rather than general types.
- const type parameter — A type parameter marked const, such as <const T>, which makes the compiler infer the exact literal types of arguments, as if the caller wrote as const.
- satisfies — An operator, value satisfies Type, that checks a value matches a type without changing the value's own, more precise inferred type.
Taught in
- Literal Types: String, Number, Boolean Literals And const — Production TypeScript
- never: The Bottom Type And Exhaustive Switch Checking — Production TypeScript
- Default Generic Arguments And Multi-Type Parameters — Production TypeScript
- Const Type Parameters: Retaining Literal Types In Functions — Production TypeScript
- Function Utilities: ReturnType, Parameters And Constructor — Production TypeScript
- Declaration Files, Ambient Namespaces And Module Augmentation — Production TypeScript
- The satisfies Operator: Safe Validation Without Widening — Production TypeScript