Syntax
interface Box<T = string> { value: T; }Examples
Default Generic Type Arguments: working example
Run this standalone example after compiling with TypeScript 5.4+ in strict mode, targeting ES2022. Expected result: The program prints READY and 4.
interface Box<T = string> { value: T; }
const label: Box = { value: "Ready" };
const count: Box<number> = { value: 3 };
console.log(label.value.toUpperCase());
console.log(count.value + 1);- Expected result: The program prints READY and 4.
- Box without an explicit argument uses string.
- Box<number> replaces the default while retaining the same container structure.
Best practices
- Choose a default that represents the common safe use case.
- Do not use any as a default merely to silence missing type information.
Runtime & versions
Compiled to JavaScript. Feature availability depends on the language and runtime version used by your project; consult the official reference below.
TypeScript Handbook