Like any other languages, TypeScript also provides basic data types to handle numbers, string
s, etc. Some common data types in TypeScript are given below.
1.number
2.
string
3.boolean
4.
enum
5.
void
6.
null
7.
undefined
8.
any
9. never
10. Array
11. tuple
Here are the few examples.
Number type:
In TypeScript, numbers are floating point values having the type as number
We can assign any numeric values including decimals, hexadecimal, binary and octal literals. But to use binary and octal literals.
let mynumber: number = 10;
String type:
When we want to use textual data, the types string
are used. This datatype use string keyword.
let name: string = "Samir";
Boolean type:
To use boolean data types in TypeScript, for declaring variables, We use boolean
keyword.
let isLogin: boolean = true;
Enum type:
Enumerated data types (enum
) are a set of numeric values with more friendly names
enum CardTypes { Debit, Credit, Virtual } let card: CardTypes = CardTypes.Debit;
Null type:
We can assign null datatype to a variable with a null keyword. We can assign an only null value to it.
let nox: null=null;
Undefined type:
We can use undefined
keyword as data type to store undefined
value to it.
let unValue: undefined = undefined;
Array type:
This datatype is used to store data in array like javascript array.
let age: number[] = [20, 35, 45]; let age: Array<number> = [20, 35, 45];
Tuple type:
// correct let user: [string, number] = ["samir", 2018]; // error let user: [string, number] = [2018, "samir"];