📝 Edit on GitHub
Type aliases
See Type aliases under Advanced Types in the docs.
Type aliases create a new name for a type. Type aliases are sometimes similar to interfaces, but can name primitives, unions, tuples, and any other types that you’d otherwise have to write by hand.
Aliasing doesn’t actually create a new type - it creates a new name to refer to that type. Aliasing a primitive is not terribly useful, though it can be used as a form of documentation.
Format
type VARIABLE_NAME = TYPE
With primitives
type Second = number;
let timeInSecond: number = 10;
let time: Second = 10;
With a union
type Fuzz = string | boolean
As a data structure
type Animal = {
name: string
}
With types
type Foo = {
name: "string"
};
type Bar = {
name: "string"
};
type Person = Foo | Bar;
With interfaces
interface Foo {
name: "string"
};
interface Bar {
name: "string"
};
type Person = Foo | Bar;