When we need a same values of type for different constants, instead of repeating the code, we can simply do as example; type Employee={name: string, id: number} let user: Employee={name:"Ram",id:10} Here, we use *type* to create an object type that can be used for other constants without repeating the code.
Null cannot be used like we use in JS. Null is hardly used and we we must use we should modify the function itself like; function name(first=string | null){console.log("Hi")} name(null); Here we defined that the value may also be a null for null to act without errors.
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 co...
Comments
Post a Comment