Posts

#12 Optional Chaining (Advanced Types)

 In optional Chaining, we perform a certain action only if it is true or valid else we do not The function gets optional. Like in Example; type boy={weight: number} function Ram(id: number): boy | null{     return id===0? null : {weight: 11} } let Employee=Ram(0); console.log(Employee?.weight); In above example, in function Ram, the type of function is given boy OR null. A variable Employee calls the function Ram with value 0 which results to null. In console, Employee?.weight signifies that If there is a value of Employee and the result is not null then show weight else ignore. In a way it is optional if the value of employee is null.

#11 Nullable Type (Advanced Types)

 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.

#10 Literal Types (Advanced Types)

 We can have a constant with two values types. Like in example; Let quantity:50 | 100=100 Here, the quantity is assigned to 50 or 100. So the value of quantity can only be 50 or 100 else it shows an error. It can also be used for strings.

#9 Intersection Type (Advanced Types)

 It is used where there is a constant which is of two or more types. It is denoted by &. It can be written as; type work={()=>void} type worker={()=>console.log("hi")} const office:{work & woker}={    work:()=>{}, worker:()=>{} } In this way, we can use both the types at the same object constant by using &

#8 Union Type (Advanced Types)

 When we need a value that may be of either types like string or number, then we may use union type. It can be written as; function( weight: number | string ){      if(typeof weight === "number"){          return weight * 2;}      else{          return parseInt(weight)*2;} Here, the weight may either be number or a string. To make the weight act as a number, we use IF where if the (type of weight === number) so that the weight can act as a number ELSE it shall act as a string.

#7 Type Alias (Advanced Types)

 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.

#6 Objects (Fundamentals of TS)

 In object in TS, it can be written as, let User:{name: string , id: number}={name: "", id:10} User.name="Ram"; Like in JS, we can modify the object. But we need to specify an empty variable inside the object prior. we can modify as given above.  OR, let User:{name?:string , id: number}={id:10} User.name="Hari" In this approach, we specify the type of the variable *if* it exists and do not have to specify an empty variable. The first approach is preferable. ________________________________________________________________________________ If we want to make a variable read only, So we dont accidently modify it, We can say as, let User:{readonly name: string , id: number}={name: "", id:10} User.name="Ram";(CANNOT BE DONE) By using readonly to a variable in an object, we then cannot modify that variable later on.