Codectionary / Developer documentation / TypeScript

Default Generic Type Arguments

A generic parameter can have a default type so callers may omit it when inference provides no better choice. Defaults must satisfy constraints, and required type parameters cannot follow optional ones. The default controls a type argument, not a runtime parameter value.

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);
  1. Expected result: The program prints READY and 4.
  2. Box without an explicit argument uses string.
  3. 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

Specifications & further reading