Type Aliases
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.
1 2 3 4 5 6 7 8 9 10 11 | type Name = string; type NameResolver = () => string; type NameOrResolver = Name | NameResolver; function getName(n: NameOrResolver): Name { if ( typeof n === "string" ) { return n; } else { return n(); } } |
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.
Just like interfaces, type aliases can also be generic - we can just add type parameters and use them on the right side of the alias declaration:
1 | type Container<T> = { value: T }; |
We can also have a type alias refer to itself in a property:
1 2 3 4 5 | type Tree<T> = { value: T; left: Tree<T>; right: Tree<T>; } |
Together with intersection types, we can make some pretty mind-bending types:
1 2 3 4 5 6 7 8 9 10 11 | type LinkedList<T> = T & { next: LinkedList<T> }; interface Person { name: string; } var people: LinkedList<Person>; var s = people.name; var s = people.next.name; var s = people.next.next.name; var s = people.next.next.next.name; |
However, it’s not possible for a type alias to appear anywhere else on the right side of the declaration:
1 | type Yikes = Array<Yikes>; // error |
Please login to continue.