Alias of types in TypeScript

Author
By Darío Rivera
Posted On in TypeScript

In a previous post we saw the Data Types in TypeScript. Today we are going to go a bit further and study type aliases, which allow us to use a data type in a much smoother way. To define a type, the reserved word type is used.


Attributes

To declare attributes we can use the following syntax. Notice how we have defined a type alias that requires two attributes, name and age, as well as their type.

type User = {
    name: string,
    age: number
}

Type aliases share many similarities with interfaces, however, aliases allow to define one or several particular types for a single variable like this:

type monthType = string | number;

interface Period {
  month: monthType
}

In this case, type aliases and interfaces are not interchangeable.

Optional attributes

It is possible to declare certain attributes or functions as optionally defined. For this, unlike other programming languages, the ? character is used in the name and not the type.

type User = {
    name: string,
    age?: number,
    addNote?: (note: string) => void
}

Read-only attributes

It is possible to define attributes that are read-only. With this, it would be impossible to try to make a reassignment to an attribute once initialized.

type User = {
    readonly type: string;
}

Attributes within a stack

If we want to define a set of possible values for an attribute, we can use the pipe character.

type User = {
    type: 'Admin' | 'Guest';
    name: string;
}

It is also possible to define an attribute with an enum type.

enum UserType {
    Admin,
    Guest
}

type User = {
    type: UserType;
    name: string;
}

To see more in detail the enum type, you can go to our post Enums in TypeScript.

Methods

To declare methods we can use two different syntaxes. Let's see below how we define the addNote method.

Form 1

type User = {
    addNote(note: string): void;
}

Form 2

type User = {
    addNote: (note: string) => void;
}

Extension

To extend a type alias, it can be done through an intersection.

type Animal = {
  name: string
}

type Bear = Animal & { 
  honey: boolean 
}

In the previous example, the Animal type alias is first defined. Subsequently, it is extended by creating the Bear type alias. The latter type requires both attributes mandatorily.

Another interesting variation of this feature is that a type can extend more than one type without even defining new attributes. In this case, it is like combining the two types into one. Let's see the following example.

type Contact = {
   name: string;
   birthDate: Date;
}

type Address = {
   line1: string;
   line2?: string;
}

type User = Contact & Address;

Type modification

Unlike interfaces, a type alias cannot be modified once created. This means that no more fields can be added to the already created alias, unless an extension is made as we previously saw. The following code would throw an error during compilation.

type Window = {
  title: string
}

type Window = {
  ts: TypeScriptAPI
}

// Error: Duplicate identifier 'Window'.

Acerca de Darío Rivera

Author

Application Architect at Elentra Corp . Quality developer and passionate learner with 10+ years of experience in web technologies. Creator of EasyHttp , an standard way to consume HTTP Clients.

LinkedIn Twitter Instagram

Sólo aquellos que han alcanzado el éxito saben que siempre estuvo a un paso del momento en que pensaron renunciar.