TypeScript 2.1を使用すると、オブジェクトのレストとスプレッドを簡単に解除して、浅いコピーを作成し、オブジェクトを新しいオブジェクトにマージできます。

オブジェクトスプレッド

オブジェクトの浅いコピーを作成します。

const names = {cat: 'Bob', dog: 'Fred', alligator: 'Benedict'};

const newNames = { ...names };

console.log(newNames.cat); // Bob

複数のオブジェクトを新しいオブジェクトにマージできます。

const names1 = {cat: 'Bob'};
const names2 = {dog: 'Fred', alligator: 'Benedict'};

const newNames = { ...names1, ...names2 };

console.log(newNames.alligator); // Benedict

スプレッドを使用するとともに、新しいプロパティを追加することもできます。

const names = {cat: 'Bob', dog: 'Fred', alligator: 'Benedict'};

const newNames = { ...names, hamster: 'Jude' };

console.log(newNames.hamster); // Jude

プロパティが複数回挿入されることになった場合、最後の1つが優先されることに注意してください。

const names = {cat: 'Bob', dog: 'Fred', alligator: 'Benedict'};

const newNames = { dog: 'Skip', ...names };

console.log(newNames.dog); // Fred

オブジェクトレスト

オブジェクトの破棄を使用して純粋なJavaScriptですでに実行できることと同様に、restはES2017のパイプラインにあるものをもたらします。

const names = {cat: 'Bob', dog: 'Fred', alligator: 'Benedict'};

const { cat, ...otherAnimals } = names;

console.log(cat); // Bob
console.log(otherAnimals); // {dog: 'Fred', alligator: 'Benedict'}