Object Rest and Spread in TypeScript

By akohad Apr18,2023

[ad_1]

const person = {
name: 'John',
age: 30,
city: 'New York',
country: 'USA'
};

const { name, ...rest } = person;

console.log(name); // Output: John
console.log(rest); // Output: { age: 30, city: 'New York', country: 'USA' }

function printPersonDetails({ name, age }: { name: string, age: number }) {
console.log(`Name: ${name}, Age: ${age}`);
}

const person = {
name: 'John',
age: 30,
city: 'New York',
country: 'USA'
};

printPersonDetails({ ...person }); // Output: Name: John, Age: 30

const person = {
name: 'John',
age: 30,
};

const address = {
city: 'New York',
country: 'USA',
};

const newPerson = { ...person, ...address };

console.log(newPerson); // Output: { name: 'John', age: 30, city: 'New York', country: 'USA' }

const person = {
name: 'John',
age: 30,
city: 'New York',
country: 'USA'
};

const newPerson = { ...person, age: 40 };

console.log(newPerson); // Output:

const person = {
name: 'John',
age: 30,
address: {
city: 'New York',
country: 'USA'
}
};

const newPerson = { ...person };

console.log(newPerson === person); // Output: false
console.log(newPerson.address === person.address); // Output: true



[ad_2]

Source link

By akohad

Related Post

Leave a Reply

Your email address will not be published. Required fields are marked *