Type aliasing

TypeScript supports defining new types via type aliases. You can use type aliases to reuse type information in multiple declarations, for example:

type person = {name: string};let employee: person;let contact: person;

Given the preceding example, the code defines a type alias named person as an object with a name key of type string. Afterwards, the rest of the code can reference this type where needed.

Type aliases are somewhat similar to interfaces (covered next), but can name primitives, unions, and tuples. Unlike interfaces, type aliases cannot be extended or implemented from, and so interfaces are generally preferred.

Unions and intersections allow you to construct a type from multiple existing types, while tuples express ...

Get Hands-On Full-Stack Web Development with ASP.NET Core now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.