⊗jstsPmEnTp 31 of 55 menu

Enum as a type in TypeScript

Each enumeration creates its own data type. Let's assign the type Season to a variable storing the current season:

let current: Season;

Let's write the season number into our variable:

let current: Season = Season.Winter; console.log(current); // 0

You can specify the season number manually:

let current: Season = 3;

But if you try to write data of a different type, for example, a string, you will get an error:

let current: Season = 'str'; // there will be an error

Unfortunately, the range of values ​​is not tracked and you can write down a number that is not in our list:

let current: Season = 7; // there will be no error

When checked using the typeof operator, our variable will return a numeric type:

let current: Season = 3; console.log(typeof current); // "number"

To sum it up, we can say that this type is not checked very strictly and therefore its value is questionable.

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