⊗jstsPmBsTA 19 of 55 menu

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:

type str = string;

Let's declare a variable using our new type:

let 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:

type stumber = string | number;

Let's declare a variable with our new type:

let test: stumber;

Let's write a number into it:

test = 123;

Let's write the following line into it:

test = 'abc';

Practical tasks

Create a new type that combines null and undefined.

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

English
AfrikaansAzərbaycanБългарскиবাংলাБеларускаяČeštinaDanskDeutschΕλληνικάEspañolEestiSuomiFrançaisहिन्दीMagyarՀայերենIndonesiaItaliano日本語ქართულიҚазақ한국어КыргызчаLietuviųLatviešuМакедонскиMelayuမြန်မာNederlandsNorskPolskiPortuguêsRomânăРусскийසිංහලSlovenčinaSlovenščinaShqipСрпскиSrpskiSvenskaKiswahiliТоҷикӣไทยTürkmenTürkçeЎзбекOʻzbekTiếng Việt
We use cookies for website operation, analytics, and personalization. Data processing is carried out in accordance with the Privacy Policy.
accept all customize decline