What are TypeScript Types?
The Type represents the different types of values which are using in the programming languages and it checks the validity of the supplied values before they are manipulated by your programs.
The TypeScript provides data types as a part of its optional type and its provide us some primitive types as well as a dynamic type “any” and this “any” work like “dynamic”.
In TypeScript, we define a variable with a “type” and appending the “variable name” with “colon” followed by the type name i.e.
let isActive: boolean = false; OR var isActive: boolean = false; let decimal: number = 6; OR var decimal: number = 6; let hex: number = 0xf00d; OR var hex: number = 0xf00d; let name: string = "Anil Singh"; OR var name: string = "Anil Singh"; let binary: number = 0b1010; OR var binary: number = 0b1010; let octal: number = 0o744; OR var octal: number = 0o744; let numlist: number[] = [1, 2, 3]; OR var numlist: number[] = [1, 2, 3]; let arrlist: Array<number> = [1, 2, 3]; OR var arrlist: Array<number> = [1, 2, 3]; //Any Keyword let list: any[] = [1, true, "free"]; list[1] = 100; //Any Keyword let notSureType: any = 10; notSureType = "maybe a string instead"; notSureType = false; // definitely a Boolean
Number: the “number” is a primitive number type in TypeScript. There is no different type for float or double in TypeScript.
Boolean: The “boolean” type represents true or false condition.
String: The “string” represents sequence of characters similar to C#.
Null: The “null” is a special type which assigns null value to a variable.
Undefined: The “undefined” is also a special type and can be assigned to any variable.
Any : this data type is the super type of all types in TypeScript. It is also known as dynamic type and using “any” type is equivalent to opting out of type checking for a variable.
You may have noticed that, I am using the “let” keyword instead of “var” keyword. The “let” keyword is actually a newer JavaScript construct that TypeScript makes available. Actually, many common problems in JavaScript are reducing by using “let” keyword. So we should use “let” keyword instead of “var” keyword.
I hope you are enjoying with this post! Please share with you friends. Thank you!!