Type Literals
Type literals allow you to create a type based on a specific string or number. This, in itself, is not very useful, but as we move on to more complex types such as union types, their use will become apparent. Literals are straightforward, so we will not spend a lot of time on them but you will need to understand the concept of literals as we move into the next phase.
Let's start by creating our string and number literals.
We will begin with a string literal:
Example01.ts
1 // string literal 2 type Yes = "yes";
Link to the preceding example: https://packt.link/96IlD.
The preceding code creates a Yes
type that will take only a specific string, "yes"
, as the input.
Similarly, we can create a number literal:
3 // number literal 4 type One = 1;
Here, we create a number literal type, One
, that will only take 1
as the input.
The basic syntax as observed in the preceding examples is quite simple. We start with the type...