A group of key value pairs
let person: {
name: string;
age: number;
} = {
name: "john",
age: 23,
};
console.log(person.name);let person: { name: string; age: number } = {
name: "john 3",
age: 40,
};We can't access the properties as typescript doesn't know the properties of the object. We can only assign an object to it.
let person: object = {
name: "john 2",
age: 33,
};
person = "string"; // Error: Type 'string' is not assignable to type 'object'.
console.log(person.name); // accessing a property will result in errorlet person: {
name: string;
age: number;
hobbies: string[];
friends: { name: string; age: number }[];
} = {
name: "John",
age: 32,
hobbies: ["Cooking", "TV"],
friends: [{ name: "Alisha", age: 37 }],
};
console.log(person.friends[0].name);List of values
// Using Generic Type
let arr: Array<string> = ["string 1", "string 2"];// Using Array type
let arr: (number | string)[] = [34, "string"];Array with specifc length and data types
let tuple: [boolean, string] = [true, "a string"];As typescript can't know the exact length of array so array methods like push, unshift, ... are exception
tuple.push("destroy");
// # but you can't push or work with wrong data type
tuple.push(3);Using readonly keyword
let tuple: readonly [boolean, string] = [true, "a string"];
// # here you can't mutate the tuple
tuple.push("destroy");Using Readonly Generic Type
let tuple: Readonly<[boolean, string]> = [true, "a string"];
// # here you can't mutate the tuple
tuple.push("destroy");