Posts

#5 Functions (Fundamentals of TS)

 While writing a function in a TypeScript, give type for the function as well as the prop. Example: function User(age: number, id: number ):number {     if(age>20){           return id+2;}     else{          return id-2} User(2,5);

#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 co...

#3 Debugging in TS

 In typescript, *Debugging is used to check if the code is working properly by going through the each line of code* A breakpoint is a location in code where the execution is paused for debugging purposes. A breakpoint is placed in VS code by clicking on the red dot left side of the lines of code. We create a launch.json by clicking the debug option and new debug. We modify the launch,json by adding a preLaunchTask to "tsc : build - tsconfig.json". We go to the debug section and press F5 or click on launch icon. We do the stepOver or F10 ko run the next line of code until the code is executed. We can look for the real time results in variables or watch section by adding the variable to look for.

#2 First TypeScript program

 Created a file index.ts In terminal ; tsc.cmd index.ts to create a js file with the code in js of index.ts. (In typeScript the name of the variable and constant should be proper like age, num and cannot be like a, x.) In typeScript once the type of a variable is given, it cannot be modified in the next line. Example: let age:number=10; age="hello"; (this is incorrect). _____________________________________________________________________________________ We use tsc.cmd --init to create a TS config file. Emit means to convert all the TS files to JS files. When a TS file is saved, the TS compiler emits an JS file corresponding to the JS code. In config file, we modified some of the configurations. =>Set rootDir to "./src" (Changes the root directory) =>Set outDir to :./Dist" (Creates an output folder for all emitted files) =>Set removeComments to "true" (Removes all the comments when emitting an JS file) => Set noEmitOnError to true (Disable...

#1 Introduction to typeScript

 Typescript is a static typing where we need to declare the type of each constant and such. Setting Up the typeScript. Installed nodeJs then in terminal ; npm i -g typescript.