TypeScript Master (50 Qs)•Part 2: Advanced Type System & Metaprogramming (Mid-Level)
What are `const` Type Parameters (TypeScript 5.0+)?
junior LevelCode ExampleTypeScript
In Simple Words:
In TypeScript 5.0, you can prefix a generic parameter with const to enforce that calls automatically infer as const literal types without forcing callers to write as const manually!
typescript
// TS 5.0+ const generic
function defineRoutes<const T extends readonly string[]>(routes: T): T {
return routes;
}
// Inferred as readonly ["/home", "/about"], NOT string[]!
const routes = defineRoutes(["/home", "/about"]);