Data types in TypeScript
Data types are the most basic yet important building block of any programming language. There are 5 data types in typescript –
Boolean
We use boolean
keyword to define a boolean value in typescript.
This most basic datatype stores
true/false
value.
How to declare a boolean type in typescript –
let isActive: boolean;
How to assign value to boolean type in typescript –
// Assign the value with the declaration
let isActive: boolean = true;
// Assign the value after the declaration
let isActive: boolean;
isActive = true;
Number
To use the numbers in typescript we use number
& bigint
keyword.
All the numbers (integers, floating-point numbers, etc) are represented by number type.
How to declare a number type in typescript –
let myAge: number;
How to assign value to a number type in typescript –
// Assign the value with the declaration
let myAge: number = 10;
// Assign the value after the declaration
let myAge: number;
myAge = 10.5;
How to declare a bigint type in typescript –
let countryPopulation: bigint;
How to assign value to a bigint type in typescript –
// Assign the value with the declaration
let countryPopulation: bigint = 100n;
// Assign the value after the declaration
let countryPopulation: bigint;
countryPopulation = 100n;
Note: While working with bigint
type in typescript, you might get the following error –
BigInt literals are not available when targeting lower than ES2020.
To fix this issue: Open the tsconfig.json
file of your project (Available at the root of your project) and update the target
property to es2020
.
"target": "es2020"
String
Strings are declared by using string
keyword.
Strings are used to store the textual data in typescript.
How to declare a string type in typescript –
let myName: string;
How to assign value to a string type in typescript –
Values can be assigned to a string type in typescript using following symbols –
- Double quote (
"
) - Single quote (
'
) - Backtick/backquote (
`
) – Can have multiple line-span and embedded expressions of the form${expression}
// Assign the value with the declaration (using double quote)
let myName: string = "Nitish Kaushik";
// Assign the value with the declaration (using single quote)
let myName: string = 'Nitish Kaushik';
// Assign the value with the declaration (using single backtick)
let myName: string = `Nitish Kaushik`;
// Assign the value after declaration
let myName: string;
myName = "Nitish Kaushik";
myName = 'Nitish Kaushik';
myName = `Nitish Kaushik`;
// Assign the value as template string
let myAge: number = 10;
let myName: string;
myName = `Nitish Kaushik with age = ${myAge}`;
The equivalent declaration is –
// Assign the value
let myAge: number = 10;
let myName: string;
myName = 'Nitish Kaushik with age =' + myAge;
Unknown
The unknown is a very interesting data type available in typescript. An unknown data type is declared using unknown
keyword.
While declaring the variable, if we don’t know the actual type for this variable then we declare it using unknown type.
Here are some of the example of unknown data type –
- Getting dynamic input from the user
- Don’t know the exact type of an API response
- etc.
How to declare an unknown type in typescript –
let notSure: unknown;
How to assign value to a unknown type in typescript –
Remember, unknowns are used for the types that we do not know. It means we can assign any type to the unknown type.
// Assign the value with the declaration
let notSure: unknown = 10;
// Assign the value after the declaration
let notSure: unknown;
notSure = 10;
// Assign different type values
let notSure: unknown;
notSure = 10;
notSure = 'Nitish';
notSure = false;
How to identify the actual type of unknown type based on the assigned value in typescript –
We can use the typeof
check to identify the type of the value assigned to this variable.
let notSure: unknown;
notSure = true;
if(typeof notSure === 'boolean'){
console.log('I am a boolean');
}
else if (typeof notSure ==='number'){
console.log('I am a number');
}
//output
I am a boolean
Any
While performing an operation on data, if you do not know the type of result then you can declare your result variable as any. To define any type, we use
any
keyword.
Any works in fully dynamic mode. It means you can assign any value, any type to it and you can also call any of its property, methods, and typescript will not throw any compile-time error.
How to declare an any type in typescript –
let anything: any;
How to assign value to a any type in typescript –
let anything: any;
anything = 'nitish';
anything = 1;
anything = true;
Learn more – What is the difference between any and unknown in typescript?
Undefined
An undefined is a type in itself and this type is defined using undefined
keyword.
We can use an undefined both as to type
and value
.
An undefined represents something that has not been initialized yet.
let myVariable:undefined = undefined;
Example –
let myVariable:any;
console.log(myVariable);
//output
undefined
While using a strict null check, an undefined can only be assigned to the unknown and any.
let myVariable1:any = undefined; // correct
let myVariable2:unknown = undefined; // correct
let myVariable3:number = undefined; // error
let myVariable4:string = undefined; // error
Null
When the value of any variable is not available then it is treated as null.
Just like the unknown, null also can be used both as type and value and can only be assigned to unknown and any.
let myVariable: null = null;
let myVariable1:any = null; // correct
let myVariable2:unknown = null; // correct
let myVariable3:number = null; // error
let myVariable4:string = null; // error
Typescript official website – https://www.typescriptlang.org/