const Type Parameters (TS 5.0)
TL;DR
TypeScript 5.0 const type parameters infer literal types instead of widened types — const T extends string[] infers ['a','b'] not string[].
Explanation
Without const, TypeScript widens inferred types: function identity<T>(x: T): T with identity(['a','b']) infers T as string[]. With const modifier: function identity<const T>(x: T): T infers T as readonly ['a', 'b']. This enables more precise inference without requiring 'as const' at the call site. Use cases: route definitions, action creators, tuple preservation. The const modifier on a type parameter is similar to inferring with as const. Works with object types too — properties become readonly with literal types.
Common Misconception
✗ const type parameters are the same as const generics in Rust — TypeScript's const type params are about literal type inference, not value-level computation.
Why It Matters
const type parameters enable APIs that preserve literal types without requiring callers to add 'as const' everywhere — cleaner DX for library authors.
Common Mistakes
- Using const type params when widened types are actually needed.
- Not combining with readonly for tuple preservation.
- Confusing with the const keyword — this is a type-level modifier.
Code Examples
✗ Vulnerable
function makeRoute<T extends string>(path: T) { return path; }
const r = makeRoute('/users'); // T inferred as string, not '/users'
✓ Fixed
function makeRoute<const T extends string>(path: T) { return path; }
const r = makeRoute('/users'); // T inferred as '/users' literal
// With arrays:
function tuple<const T extends unknown[]>(...args: T): T { return args; }
const t = tuple(1, 'two', true); // [1, 'two', true] not (number|string|boolean)[]
Tags
🤝 Adopt this term
£79/year · your link shown here
Added
23 Mar 2026
Views
31
🤖 AI Guestbook educational data only
|
|
Last 30 days
Agents 0
No pings yet today
No pings yesterday
Amazonbot 15
Google 4
Perplexity 4
Unknown AI 3
Ahrefs 3
ChatGPT 1
Meta AI 1
Also referenced
How they use it
crawler 27
crawler_json 2
pre-tracking 2
Related categories
⚡
DEV INTEL
Tools & Severity
🔵 Info
⚙ Fix effort: Low
⚡ Quick Fix
Add const modifier to type parameters when you want literal type inference. Useful for route builders, action creators, and tuple-returning utilities.
📦 Applies To
typescript 5.0
web
cli
🔗 Prerequisites
🔍 Detection Hints
<const T
Auto-detectable:
✗ No
typescript
🤖 AI Agent
Confidence: Low
False Positives: High
✗ Manual fix
Fix: Low
Context: Function