Skip to content

Instantly share code, notes, and snippets.

@hhyyg
Last active February 15, 2019 05:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hhyyg/87c760941947aa7216d764bc9237609f to your computer and use it in GitHub Desktop.
Save hhyyg/87c760941947aa7216d764bc9237609f to your computer and use it in GitHub Desktop.
TypeScript: Spread Operator(Object spread, Destructuring)
interface PersonA {
name: string,
group: string,
item1: string,
item2: string,
}
interface PersonB {
name: string,
group: string,
itemOne: string,
itemTwo: string,
}
const personA: PersonA = {
name: 'name',
group: 'group',
item1: 'item1',
item2: 'item2',
}
const {
item1,
item2,
...partialPersonA
} = personA;
const personB: PersonB = {
...partialPersonA,
itemOne: personA.item1,
itemTwo: personA.item2,
}
console.log(personB)
/*
group: "group"
itemOne: "item1"
itemTwo: "item2"
name: "name"
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment