Typescript in 5 mins
Have you ever heard about Typescript. Well, these days I am hearing a lot about it. All the interviews which I attend asks me, whether am I familiar with Typescript or not ? Though it was not mandatory in most cases, but i was fed up listening this term called ‘Typescript’.
What does a Static Type Checker do ?
Typescript checks the program before execution. It detects the errors before it executes. consider the below example :
Here in alert the value of data.name is displayed as “undefined”.
Here in the above TS code, it throws error stating that Property name does not exist in the object data.
The above explains how TS is checking for type of the variable. color variable’s type is declared as string. and given a value orange, later, the color variable is assigned a value with type number. TS here expects the value of color should always be string and hence throws error Type ‘number’ is not assignable to type ‘string .
There are many good concepts to learn if we go through the Typescript Site. A few important topics to remember and points to be noted …
- The three most common primitives are : number, string, boolean.
- Type annotations on variables
- Parameter Annotations
- Return type Annotations
- Object types and Optional properties
- Union Types , for defining different Types to a single variable at initialisation.
- ‘type’ aliases
- Interfaces : note : Interfaces and Type Aliases are almost equal, the only difference is Interface is extendable, where Type Alias structure cannot be extended. In short, a type cannot be changed after creation, while an Interface can.
- classes and so on …
Hope you enjoyed this small introduction of ‘Typescript’. Do follow for more updates.