You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
TypeScript is very Important when you are worked on the enterprise level of application. You will learn in this module are integrate the TypeScript with the React Js and Node Js
constmyArr: Array<number>=[12,90,71];```- If the data types inside the array are unknown or a mixture of data types, the array can be declared using the <any> type (this is a type all on it's own that is discussed below):```tsconstmyArr: Array<any>=[12,'thirteen',false];
If you want to return number in function so You will do this manner
Tuple is tightly coupled Array. If you mentioned limited array type so you will not add more. so one kind of you also set the length of the array.
constperson: {name: string,company: string,position: string,expectationCtc: number,role: [number,string]}={name: "Darshit",company: "virtusa",position: "Engineer of Technology",expectationCtc: 25,role: [22,"Test"]// Also You Follow the Order wherever you mention in object}person.role=[32,"ssdd","ddd"];|->ItThrowtheErrorBecauseithamperthelengthoftherolethatarementionedonabove
push method is exception of the tuple means you are able to add object more that mentioned lengh
Ex.
person.role.push("sjkhs");/-> It works Perfectly point be note
All Types in Type Script
Union Types in Type Script
Below is the Union method so we use both parameter like number and String
Unknown Type before assign the value first it will check the value Type then after it will assign the value
and Unknown not all every values, it will allow certain types
instead of Any allow the all the types
letuserName: unknown;letuserPassword: string;userName="darshitHGAaja";userPassword="2872@@@873181";userPassword=userName;// userName type is unknown so it will not assign to the userpassword// so assigning unknown value to string we need to check first then after we will assignconsole.log(typeofuserName);if(typeofuserName==="string"){userPassword=userName;}
Never use in TypeScript
Never is used for standard enterprise term
Let say if you want not return something so for that you used Never Type in TypeScript
we will use void for that but as standard way we will use Never use
letsomething: void=null;letnothing: never=null;// Error: Type 'null' is not assignable to type 'never'functionthrowError(errorMsg: string): never{thrownewError(errorMsg);}functionkeepProcessing(): never{while(true){console.log("I always does something and never ends.");}}
classDepartment{name: string;constructor(n: string){this.name=n;console.log("My Name is Khan",this.name);}}constaccounting=newDepartment("Acconting vala Bhai");console.log(accounting);
Constructor Function and this keyword
classDepartment{name: string;constructor(n: string){this.name=n;}describe(this: Department){returnthis.name;}}constaccounting=newDepartment("Hi Darshit!");accounting.describe();// this weill display OutputconstaccountInfo={name: "Dummy",describe: accounting.describe};// but if you doing accountInfo.describe -> It will display Undefinedconsole.log("accountInfo: ",accountInfo.describe());// accountInfo: Dummy
private and public identifier define in class
classDepartment{privateemployeeId: string;privateemployeeName: string;publiccompanyWiseEmployeeNames: {companyName: string;employeeName: string;}[]=[];constructor(name: string){this.employeeName=name;}addEmployee({ id }){this.employeeId=id;}getEmplyeeInfo(): string{return`My Employee ID is ${this.employeeId}. My Name is ${this.employeeName}`;}addEmployeeCompanyWise(addComWithEmpName){this.companyWiseEmployeeNames.push(addComWithEmpName);}getCompanyEmployees(){returnthis.companyWiseEmployeeNames;}}constperson=newDepartment("Darshit");person.addEmployee({id: "12EDx23"});person.addEmployeeCompanyWise({companyName: "E-Delta",employeeName: "Darshit Gajjar",});person.addEmployeeCompanyWise({companyName: "Virtusa",employeeName: "Darshit Gajjar",});// If you not define so by default it is consider as the publicperson.companyWiseEmployeeNames.push({companyName: "Mandiant",employeeName: "Darshit Gajjar",});// This Became True if and only if you're variable is objectconsole.log(person.getCompanyEmployees());// [ { companyName: 'E-Delta', employeeName: 'Darshit Gajjar' }, { companyName: 'Virtusa', employeeName: 'Darshit Gajjar' },{ companyName: 'Mandiant', employeeName: 'Darshit Gajjar' } ]console.log(person.getEmplyeeInfo());// My Employee ID is 12EDx23. My Name is Darshit
private readOnly Identifier
classDepartment{privatereadonlyemployeeName: string;// This kind of Extra thing make your code more clear either work on big team or after come back to couple of weeks. so this is Best Practicesconstructor(name: string){this.employeeName=name;}getEmplyeeInfo(): string{return`My Name is ${this.employeeName}`;}}constperson=newDepartment("Darshit");console.log("Hi",person.getEmplyeeInfo());
Interface not give conckrit example, But through Interface you will be able to multiple interface implment in class.
In class if you use interface, so we have add some more function and variables. so It give more freedom. But with freedom you have to use method and variables that mentioned in class.
Beauty of Interface is we use in Various classes
Interface super powerful If certain method want to use in strict manner
ex. we have one method that is super useful and we are used in various classes so that time, this Interface super helpful to strickly follow that method
interfacePerson{empName: string;setPersonName(name: string): void;}interfaceGetUserInfo{getUserName(): string;}classEmployeeimplementsPerson,GetUserInfo{organisationName: string;empName: string;constructor(org: string){this.organisationName=org;}setPersonName(name: string): void{this.empName=name;}getUserName(): string{return`${this.empName} work in ${this.organisationName}`;}}constuser=newEmployee("Virtusa");user.setPersonName("Darshit Gajjar");console.log(user.getUserName());
Read Only Interface Functionality
Read only Functionality super useful, When you are not want to modify variable value in externally, so that time it work like charm. so It prohibated to user to modify it's value.
interfacePerson{readonlyempName: string;setPersonName(name: string): void;}interfaceGetUserInfo{getUserName(): string;}classEmployeeimplementsPerson,GetUserInfo{organisationName: string;empName: string;constructor(org: string){this.organisationName=org;}setPersonName(name: string): void{this.empName=name;}getUserName(): string{return`${this.empName} work in ${this.organisationName}`;}}constuser=newEmployee("Virtusa");user.setPersonName("Darshit Gajjar");user.empName="DarshitGajjar";// It throw the Error like use haven't assign the value because it's read-only properties.console.log(user.getUserName());
Multiple Interface use Together
we have multiple class Application
class1 need only name
class2 need name and firstName
class3 need name,firstName and lastName
So in this scenario we have to use the multiple Interface together
ex.