Type Aliases
Type aliases give a reusable name to any type. They are fully transparent — interchangeable with the type they refer to.
1type ID = number;2const userId: ID = 42;When to use this
Use type aliases to give descriptive names to complex or frequently used types. Since aliases are transparent, any code that accepts the underlying type also accepts the alias and vice versa. When you need to hide the underlying type from consumers of a module, use opaque type aliases instead. Interfaces, classes, and enums also introduce named types with additional capabilities beyond simple naming.
Type Alias Syntax
Type aliases are created using the keyword type followed by its name, an
equals sign =, and a type definition.
type Alias = Type;
Any type can appear inside a type alias.
1type NumberAlias = number;2type ObjectAlias = {3 property: string,4 method(): number,5};6type UnionAlias = 1 | 2 | 3;7type AliasAlias = ObjectAlias;Type Alias Generics
Type aliases can also have their own generics.
1type MyObject<A, B, C> = {2 property: A,3 method(val: B): C,4};Type alias generics are parameterized. When you use a type alias you need to pass parameters for each of its generics.
1type MyObject<A, B, C> = {2 foo: A,3 bar: B,4 baz: C,5};6
7const val: MyObject<number, boolean, string> = {8 foo: 1,9 bar: true,10 baz: 'three',11};See Also
- Opaque Type Aliases — type aliases that hide their underlying type outside of the defining file
- Generics — parameterized types used with type aliases and functions
- Interfaces — another way to define reusable types, with structural (shape-based) checking for class instances