Number :
It is used for both
integers
andfloating-point numbers
representation.
let val1: number = 26;
let val2: number = 42;
// We can also use number without specifying it's type
// like -
let val3 = 64;
String :
It represents the sequence of
characters
(alphabet, digit, symbols) but with double quotes.
let name: string = "Lakshman76";
let greeting: string = `Namaste ${name}`;
name = 42; // Warning - attempts to re-assign the value to a different type
Boolean :
It is used only to represent logical values -
true
andfalse
.
const isLoggedIn: boolean = false;
const isActive: boolean = true;
Array :
It is used to store multiple values of the same data type.
const names: string[] = ["Radha", "Krishna"];
const evens: number[] = [2, 4, 6, 8, 10];
const mix: (string | number)[] = [123, "RAM", 423, "hello"];
// 2D Array
const numList: number[][] = [[1, 2, 3, 4], [5, 6, 7, 8]];
Tuple :
A
tuple
is a typed array with a pre-defined length and type for each index.
const user: [string, number, boolean];
user = ["Laksh", 20, true]; // value should be in this order
Void :
It is generally used in function return type when a function returns nothing.
function greeeting(msg: string): void {
console.log(msg);
}
greeting("Welcome to TYPESCRIPT");
Undefined :
It represents an
undefined
value or the value given to uninitialized variables.
let message;
console.log(message); // undefined
message = undefined;
console.log(message); // undefined
Null :
It represents a
null
value or when an object doesn’t have any value.
let empty: null = null;
console.log(empty); // null
Any :
If a variable is declared with
any
type, those variables can hold any type of value.NOTE:- It is recommended to not use
any
.
let value: any;
value = "Hello ji";
console.log(value); // Hello ji
value = 45;
console.log(value); // 45
Enum :
It is used to define the set of constant values.
By default, enums will initialize the first value to
0
and add1
to each additional value:
enum count {
zero,
one,
two,
three,
four,
}
const val: count = count.two;
console.log(val); // 2
enum anotherCount {
zero = 5,
one,
two,
three,
four,
}
const val1: anotherCount = anotherCount.one;
console.log(val1); // 6
There are two types of enum
- a) number b) string
Number :
We can set the value of the first numeric enum and have it auto-increment from that. And also
We can assign unique number values for each enum value, and then the values will not incremented automatically:
enum anotherCount {
zero = 5,
one,
two,
three,
four,
}
const val1: anotherCount = anotherCount.one;
console.log(val1); // 6
enum StatusCodes {
NotFound = 404,
Success = 200,
Accepted = 202,
BadRequest = 400
}
console.log(StatusCodes.NotFound); // 404
String :
Enum can also contain
strings
. This is more common than numeric enums, because of their readability and intent.
enum directions {
North = 'North',
East = "East",
South = "South",
West = "West"
};
console.log(directions.North); // North
Never :
It represents values that never occur, such as a function that never returns or always throws an error.
function throwError(message: string): never {
throw new Error(message);
}
Union :
It allows a variable to hold more than one type of value.
let multiple: number | string;
multiple = 123;
console.log(multiple); // 123
multiple = "ABC";
console.log(multiple); // ABC
Type aliases :
It is used to create custom types using the
type
keyword.
type User = {
name: string;
email: string;
isActive: boolean;
};
const newUser: User = {
name: "Laksh",
email: "laksh@dev.com",
isActive: true
}
console.log(newUser); // { name: "Laksh", email: "laksh@dev.com", isActive: true }