TypeScript Reference ยท Keywords and Declarations
TypeScript type aliases
Reference for the TypeScript type keyword: declaring aliases for unions, objects, tuples, functions and generics, recursive aliases, and how they differ from interfaces.
The type keyword gives a name to any type expression. The alias is not a new type; it is just another name, so type UserId = string is fully interchangeable with string. Aliases exist only at compile time and produce no JavaScript.
Unlike an interface, an alias can name anything: a union, a tuple, a function signature, a primitive, a mapped or conditional type. Aliases can take type parameters and can refer to themselves.
type Id = string | number; type Point = { x: number; y: number }; type Pair<T> = [T, T];
Try it
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.
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
type Name = TypeExpression; type Name<T, U extends object = {}> = TypeExpression; export type Name = TypeExpression;
| Part | Description |
|---|---|
Name |
The alias name. By convention it starts with a capital letter. |
<T, ...> |
Optional type parameters, with optional constraints (extends) and defaults (= ...). |
TypeExpression |
Any type: primitives, literals, unions, intersections, object types, tuples, function types, mapped, conditional and template literal types. |
Errors and edge cases:
- Declaring the same alias name twice in one scope is error TS2300 ("Duplicate identifier"); aliases do not merge, unlike interfaces.
- An alias that refers directly to itself without an object, array, tuple or function wrapper is error TS2456 ("Type alias circularly references itself"). Recursive references inside object types, arrays, tuples and (since TypeScript 3.7) generic type arguments are fine.
- Using a generic alias without its required type arguments is error TS2314 ("Generic type 'Pair' requires 1 type argument(s)").
Usage notes
- Alias versus interface. Both describe object shapes. Use
typefor unions, tuples, function types and computed types; useinterfacewhen you want declaration merging or readableextendserror messages. See type aliases vs interfaces. - Aliases do not create nominal types.
type Meters = numberandtype Seconds = numberare mutually assignable. Use branding (an intersection with a unique marker) when you need them distinct. - Naming in errors. The compiler often prints the alias name, but it may expand it, so do not depend on the exact wording.
- Intersections replace
extends.type B = A & { extra: string }combines shapes. Conflicting property types intersect tonever. - Exporting. Use
export type(orimport type) for aliases that other files use. WithisolatedModules, re-exporting an alias needs thetypemodifier. - No runtime value. An alias cannot be used in an expression. To get the type of a value, write
type T = typeof value. - Generics. Defaults and constraints work as on functions:
type Box<T = string> = { value: T }.