Skip to content

Instantly share code, notes, and snippets.

@kerren
Last active March 22, 2021 19:21
Show Gist options
  • Save kerren/76114345d96a676aa904839b98446668 to your computer and use it in GitHub Desktop.
Save kerren/76114345d96a676aa904839b98446668 to your computer and use it in GitHub Desktop.
[The spread operator] A simple example to show how it works #spread #operator #typescript
const a = {
character: 'a',
description: 'This is a',
level: 1,
aUnique: 123
};
const b = {
description: 'This is b',
bbb: 'b'
};
const c = {
test: 'c'
};
const spread = {
...a,
...b,
level: c.test
};
console.log(spread);
/**
* {
* character: 'a',
* description: 'This is b',
* level: 'c',
* aUnique: 123,
* bbb: 'b'
* }
*/
// There's something else that's important to remember. If you spread null or undefined it doesn't do anything in the object!
console.log({...null});
// {}
console.log({...undefined});
// {}
// This is applicable if you do something like:
const test = {...myStore.user, loggedIn: true };
// And maybe at that stage myStore.user was actually undefined
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment