Q75: What are `const` Type Parameters introduced in TypeScript 5.0?
Prefixing a generic parameter declaration with const (<const T>) forces TypeScript to infer narrow, literal types for object and array arguments passed into functions by default, eliminating the need for callers to manually append as const.
const Generic Parameter Inference
function setHashes<const T extends readonly string[]>(names: T): T {
return names;
}
// Inferred automatically as readonly ["alice", "bob"] rather than string[]!
const res = setHashes(["alice", "bob"]);💡 const T instructs the compiler to infer literal types automatically without requiring explicit 'as const' assertions at the call site.
Key Takeaways
- Infers narrow literal types for generic arguments without requiring manual
as constassertions.