What are const type parameters in TypeScript 5.0?
Answer
Const type parameters (TypeScript 5.0) allow you to mark a generic type parameter with const, causing TypeScript to infer the narrowest (most specific) type for that parameter — equivalent to adding as const to the passed argument. Without const: function identity<T>(value: T): T { return value; } const result = identity(["a", "b"]); — T is inferred as string[], result type is string[]. With const: function identity<const T>(value: T): T { return value; } const result = identity(["a", "b"]); — T is inferred as readonly ["a", "b"], the narrowest possible type. This is particularly useful for functions that work with literal values and need to preserve the exact type without requiring callers to add as const everywhere. Common use case: type-safe routing, event systems, or any API where the literal type of arguments matters for the return type's correctness.
Previous
What is the difference between unknown and {} and object in TypeScript?
Next
What is the using keyword in TypeScript 5.2?
More TypeScript Questions
View all →- Advanced What is structural typing in TypeScript?
- Advanced What are branded types (opaque types) in TypeScript?
- Advanced What is the Variance annotation in TypeScript 4.7?
- Advanced What is the satisfies operator used for in TypeScript?
- Advanced How do you implement a deep partial type in TypeScript?