← CodeClarityLab Home
Browse by Category
+ added · updated 7d
← Back to glossary

const Type Parameters (TS 5.0)

typescript 5.0 Intermediate

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)[]

Added 23 Mar 2026
Views 31
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings W 0 pings T 0 pings F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F 1 ping S 1 ping S 0 pings M 0 pings T 1 ping W 0 pings T 1 ping F 2 pings S 0 pings S 0 pings M 0 pings T 0 pings W 1 ping T 1 ping F 1 ping S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T
No pings yet today
No pings yesterday
Amazonbot 15 Google 4 Perplexity 4 Unknown AI 3 Ahrefs 3 ChatGPT 1 Meta AI 1
crawler 27 crawler_json 2 pre-tracking 2
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

✓ schema.org compliant