TypeScript Reference ยท Generics
const type parameters in TypeScript
Reference for TypeScript const type parameters: infer literal and readonly types from arguments without writing as const, with limits and constraint rules.
A const type parameter tells the compiler to infer the argument as if it had been written with as const: string literals stay literals, object properties become readonly, and arrays become readonly tuples. You write the modifier before the parameter: <const T>.
Without it, inference widens: ["a", "b"] becomes string[] and { mode: "dark" } becomes { mode: string }. With it, the caller does not have to remember as const at every call site, and the function can offer precise types for configuration objects, routes, event lists and similar data. The feature was added in TypeScript 5.0.
function tuple<const T extends readonly unknown[]>(items: T): T { return items; } const t = tuple(["a", 1]); // readonly ["a", 1]
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.
Syntax
function f<const T>(value: T): T function g<const T extends readonly unknown[]>(value: T): T class Box<const T> { constructor(public value: T) {} } type Fn = <const T>(value: T) => T;
| Part | Description |
|---|---|
const |
Modifier on a type parameter; the argument is inferred with as const semantics. |
extends readonly X[] |
For arrays, allow readonly arrays in the constraint so the readonly tuple that const produces is accepted. |
| Where | Functions, methods, classes and function type aliases. Not allowed on interface or type alias parameters. |
Edge cases: the modifier only affects arguments written as object, array or primitive literals directly at the call. Variables declared earlier keep their declared type.
Usage notes
- Use readonly array constraints. A
constparameter produces readonly tuples, so writeT extends readonly string[], notT extends string[], to match what is inferred. - Not retroactive.
const names = ["a", "b"]; exact(names)yieldsstring[], becausenameswas already widened. - Nested literals are covered. Objects inside arrays inside objects all become readonly with literal types.
- Returns are readonly. Code that wants to mutate the result needs a copy, such as
[...b]. - Alternative:
as const. The caller writesexact(["x", "y"] as const);const Tmoves that burden into the function declaration. - Alternative:
extends string.<T extends string>also preserves string literals for a single value, but it does not turn arrays into tuples. - No runtime effect. The emitted JavaScript is unchanged; the arrays are not frozen.
- Version. Requires TypeScript 5.0 or later; older versions report a syntax error.