Type Aliases in TypeScript

In TypeScript, you can create aliases types. This is done with the type operator.

As an example, let's define another name for the string data type:

typescripttype str = string;

Let's declare a variable using our new type:

typescriptlet test: str = 'abc';

In general, renaming standard types does not bring any practical benefit. Let's look at a more useful example of using aliases.

Application

Let's create a new data type using type union:

typescripttype stumber = string | number;

Let's declare a variable with our new type:

typescriptlet test: stumber;

Let's write a number into it:

typescripttest = 123;

Let's write the following line into it:

typescripttest = 'abc';

Practical tasks

Задание 1

Create a new type that combines null and undefined.

Задание 2

Create a new type that combines the boolean type, null, and undefined.