#4 Built in Types in TS (Fundamentals of TS)
There are different Types in TS which includes of JS and extends to:
number string boolean null undefined object any unknown never enum tuple
Example:
Let age : number= 18;
here number is the type for age.
ANY TYPE
If we write just age; it becomes an any type automatically because the type of the variable is not given.
The type "any" is mainly avoided because due to this, the purpose of a typescript loses its value.
While in arrays; Use;
let num : number[]= [1,2,3,4];
TUPLES Type
If we need to have an array with different types then tuples is used.
It can be used as:
Example:
let user:[number, string]=[2,"hello"]
It is usually used for 2 values of array. If the elements of an array exceeds the elements of type then there cause an error.
Example:
let user:[number, string]=[2,"hello",5]
ENUMS Type
If we need to add more than one constants, we use Enums. It makes the code easier to read and follow.
Example:
const enum Size{Small="s", Medium="M", Large="L"}
let User=Size.Small
(Value of user is s)
Here, we declare the type and value of constants inside the {} and can be used later on.
Comments
Post a Comment