Skip to content
Tutorials Center
My learning

Topics

TypeScript Reference ยท Generics

Generic type aliases in TypeScript

Reference for TypeScript generic type aliases: parameterize unions, object shapes, function types and conditional types, with defaults and constraints.

A generic type alias is a type declaration with type parameters: type Name<T> = .... It is a function from types to types. You give it type arguments and it produces a new type, which makes it the right tool for reusable unions, wrappers, function shapes and anything built with mapped or conditional types.

Unlike interfaces, an alias can name any type, not only object shapes, so many of the most useful generic helpers (Nullable<T>, Result<T, E>, Callback<T>) are aliases.

TypeScript
type Nullable<T> = T | null;
type Pair<A, B = A> = [A, B];

Try it

TypeScript Unions, tuples and functions as generic aliases

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.

TypeScript Mapped and conditional aliases

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.

Syntax

TypeScript
type Name<T, U extends Constraint = Default> = TypeExpression<T, U>;
Name Description
T, U Type parameters, usable anywhere on the right-hand side.
extends Constraint Optional limit on accepted arguments.
= Default Optional default; parameters with defaults must come last.
TypeExpression Any type: union, intersection, tuple, function, object, mapped, conditional, template literal.

Edge cases: using the alias without its required arguments is error TS2314. An alias may refer to itself inside an object, array, tuple or function member (Tree above), but a direct circular reference such as type A<T> = A<T> is an error.

Usage notes

  • Alias versus interface. Pick an alias for unions, tuples, function types and computed types; pick an interface for object shapes that other code may extend or merge.
  • Aliases are transparent. Nullable<string> is just string | null; the alias name may disappear from hover text and error messages.
  • Distribution. When the right-hand side is a conditional type on a naked parameter, unions are processed member by member (ElementOf<string[] | number[]> is string | number).
  • Defaults reduce noise. Result<number> is friendlier than Result<number, string> everywhere.
  • Labeled tuple elements (first: A) document positions and have no runtime effect.
  • No partial application. You cannot fix one parameter and leave another open; define a second alias such as type StringResult<T> = Result<T, string>.
  • Aliases are erased. They never exist at runtime and cost nothing in the compiled JavaScript.