Last active
March 22, 2021 19:21
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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