TypeScript Generics and Advanced Type Operators
const type parameters — literal-friendly inference
A const modifier on a type parameter asks the checker to prefer literal-preserving inference for suitable inline expressions. It does not freeze values, change JavaScript output, or recover literal information already widened in a variable. A mutable constraint may prevent the preferred readonly candidate from satisfying the bound. Use a readonly constraint when a tuple-like input should retain readonly inference. Example output: "draft,ready". Local strict TypeScript 5.8.3 / ES2022 baseline. The type construct itself is erased; the logged value illustrates a checked use, not runtime type validation.
Formal Syntax & Structure
typescript syntaxtypescript
function capture<const T>(value: T): T { return value; }Working Example
Example Snippettypescript
function capture<const T extends readonly string[]>(items: T): T { return items; }const captured = capture(["draft", "ready"]);const exact: readonly ["draft", "ready"] = captured;console.log(exact.join(","));