In this post we are going to discuss about basic types in TypeScript. As we all know that typescript is a typed super-set of JavaScript that compiles to plain JavaScript. The key feature which separate typescript with JavaScript is that typescript supports some basic types. Thus TypeScript's type analysis occurs entirely at compile-time and adds no run-time overhead to program execution.
The Primitive Types in TypeScript are 'number', 'boolean', 'string' and 'array'. Now you might say that 'boolean' and 'array' are there in JavaScript so whats new in typescript ? Actually in JavaScript we do not explicitly defined 'boolean' and 'array' variables. In TypeScript we have an option to actually assign an actual number or boolean or any type to that variable. The importance of that is, if I assign a wrong type we can get to know upfront rather than debugging it.
There are two additional basic types which you can come across and they are 'any' and 'void'. 'Any' actually means that it can do anything, literally it means that you can assign it anything.
This is useful when you are getting dynamic values and when you want to pass through compile-time checks. 'Void' is somewhat opposite of 'any'. Actually it is useful when your function is not returning anything. 'Void' type is a sub-type of the 'Any' type and a super-type of the 'Null' and 'Undefined' types.
Thus unlike JavaScript we have to give variable name and then instead of just saying equal, we need to assign a type (number, boolean, string or array).
var variableName: variableType = value;
The TypeScript types are defined in a file called lib.d.ts a.k.a Library Definition Files. One more thing to note here is that all types in TypeScript are subtypes of a single type called 'Any' Type whereas 'Null' type is a sub-type of all types, except the 'Undefined' type. This means that null is considered a valid value for all the primitive types.
Please Like and Share the Blog, if you find it interesting and helpful.
The Primitive Types in TypeScript are 'number', 'boolean', 'string' and 'array'. Now you might say that 'boolean' and 'array' are there in JavaScript so whats new in typescript ? Actually in JavaScript we do not explicitly defined 'boolean' and 'array' variables. In TypeScript we have an option to actually assign an actual number or boolean or any type to that variable. The importance of that is, if I assign a wrong type we can get to know upfront rather than debugging it.
There are two additional basic types which you can come across and they are 'any' and 'void'. 'Any' actually means that it can do anything, literally it means that you can assign it anything.
This is useful when you are getting dynamic values and when you want to pass through compile-time checks. 'Void' is somewhat opposite of 'any'. Actually it is useful when your function is not returning anything. 'Void' type is a sub-type of the 'Any' type and a super-type of the 'Null' and 'Undefined' types.
Thus unlike JavaScript we have to give variable name and then instead of just saying equal, we need to assign a type (number, boolean, string or array).
var variableName: variableType = value;
The TypeScript types are defined in a file called lib.d.ts a.k.a Library Definition Files. One more thing to note here is that all types in TypeScript are subtypes of a single type called 'Any' Type whereas 'Null' type is a sub-type of all types, except the 'Undefined' type. This means that null is considered a valid value for all the primitive types.
Please Like and Share the Blog, if you find it interesting and helpful.
No comments:
Post a Comment