Setting up typescript into your workplace.
Open your command prompt and follow the below commands.
1.Install typescript globally.
npm install -g typescript
2. let's make a new folder and open it with CS Code
3. let's compile the main.ts typescript code.
after the compilation, there is a main.js file as the result. If you want to check it out, you can type the “dir” command in prompt
4.now you will able to see the main.js file in the files list. Then open the main.js file using the command prompt(“code main.js”)
5. You can run this file using node
That is the basic way of compiling and practicing with typescript
We can use two keywords to declare a variable. I hope to give you a basic idea about the difference between let and var.
Variables which define using ‘var’ keyword scope to the nearest function.
the result will like this.
Variables which define using ‘let’ keyword scope to the nearest block. according to that above code will show you an error. but it gives the previous result.
but the result will be the same as var.
There are few data types in TS. Those are ‘number, string, boolean, any’ likewise. except that we can declare ‘number array’ s and ‘any array’. You can declare variables with the type of each variable.
But those issues can minimize using TS. We can identify those errors easily in TS.
according to the above image, you can see some cofusion happened in TS. First, we have defined ‘a’ and its type as ‘any’. But in the third line, we have assigned string to ‘a’ variable. We can smooth this situation using type assertion.
I have used two ways of type assertions. As we can see in line 5, using ‘ < string >’ or using ‘(variable as string)’.
In TS, we can define functions as Arrow functions. At that time, we can drop the function keyword. If you have used C#, you may have seen this type of function. Those are called Lambda functions. Let's see how to define…
The first method is the normal way of defining a function with ‘function’ keyword. But the arrow function has been defined with ‘=>’ in the image.
If we have to pass few parameters to a function, instead of using inline annotation, we can use interface as a most suitable way. This may help to use those parameter values easily in related functions.
we can use interface like shown in below image,
This way is much cleaner and re-useable.
Basically, a class means in TS, Group variables(properties), and functions (methods) that are highly related.
Those drawPoint function and get Distance function are related to each other. So we can group those as a class in TS like this.