python ruby-programming-language Smalltalk Vue.js Knockoutjs TypeScript JavaScript, OOPs JavaScript Angular 8,7,6,5,4 and 2 Interview Questions and Answers Angular 8,7,6,5,4 and 2 Interview Questions and Answers Angular 4 Angular 2 AngularJs 1.x NodeJs Perl Programming R Programming RequireJs Rust Programming Backbonejs closure programming language go-programming-language kotlin-programming-language
Angular 2 async pipe with ngFor loop

What are Types in TypeScript?

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.

Stayed Informed – Learn Angular 2

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.

A note about “let” keyword –

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!!

Hi there! Welcome to my blog. My name is Anil Singh. I'm a author of this blog. For more detail about me, kindly refer to this link..
My Blogs - and
My Books - Interview Questions and Answers Books- Get Your Book in 15+ Digital Stores Worldwide..

You Might Also Like Powered by Blogger.
ASK Questions