⊗jstsPmBsSLT 20 of 55 menu

String literal type in TypeScript

With the help of the type union operator, you can combine not only built-in types, but also strings. For example, let's make it so that a variable can only take one of two string values:

let str: 'success' | 'error';

Let's check. Let's write a variable one of the lines:

str = 'success';

Let's write the second one:

str = 'error';

But trying to write a different line will result in an error:

str = 'eee'; //

Make the variable take one of three values: 'error', 'warning', or 'success'.

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