๐ Incorrect (variable) is specified more than once, so this usage will be overwritten ์๋ฌ ํด๊ฒฐํ๊ธฐ
typescript๋ก ์์ ํ๋ค ๋ค์๊ณผ ๊ฐ์ ์ค๋ฅ๋ฅผ ๋ง์ฃผํ๋ค.

์ค๋ฅ๊ฐ ๋ฐ์ํ ์์ธ์ getListHref
๋ฅผ props๋ก ๋ด๋ ค์ค ์์น๊ฐ ์๋ชป๋์๊ธฐ ๋๋ฌธ์ธ๋ฐ, ๋ฐ๋ก {...props}
์์ ์ ์ธํ๊ธฐ ๋๋ฌธ์ด๋ค. ํ๋จ์ AS-IS
์ฝ๋๋ฅผ ์ดํด๋ณด๋ฉด {...props}
์์ getListHref
๋ฅผ ์ ์ธํ๋๋ฐ, ์ด๊ฒ์ ์๋ฏธ๊ฐ ์๋ค. ์๋ํ๋ฉด ...props
๋ก ๋ด๋ ค์ค๋ getListHref
์ ์ต์ข
์ ์ผ๋ก ์ ์ฉํ๊ธฐ ๋๋ฌธ์ ์ฒ์์ ์ ์ธํ getListHref
๋ฅผ ํญ์ overrideํ๊ธฐ ๋๋ฌธ์ด๋ค.์ด๋ด ๋๋ TO-BE
์ฝ๋์ฒ๋ผ ๋ด๊ฐ ์ ์ฉํ๊ณ ์ถ์ getListHref
๋ฅผ {...props}
๋ค์ ์ ์ธํด์ฃผ๋ฉด ์ค๋ฅ๋ฅผ ํด๊ฒฐํ ์ ์๋ค.
// AS-IS
export function createHeader({ width }: { width: SemanticWIDTHS }) {
return function Header(props: ComponentProps<typeof HeaderBase>) {
const getListHref = (queries: string) => `?${queries}`
return <HeaderBase getListHref={getListHref} {...props} /> // 'getListHref' is specified more than once, so this usage will be overwritten.
}
}
// TO-BE
export function createHeader({ width }: { width: SemanticWIDTHS }) {
return function Header(props: ComponentProps<typeof HeaderBase>) {
const getListHref = (queries: string) => `?${queries}`
return <HeaderBase {...props} getListHref={getListHref} />
}
}
์ ์ดํด๊ฐ ๋์ง ์๋๋ค๋ฉด ๋ค์ ์ฝ๋๋ฅผ ๋ณด์. Bad
์ ์ ์ธํ otherInfo.age
๋ ์ ์ฉ๋์ง ์๋๋ค. ์๋ํ๋ฉด ...myInfo
์ property๊ฐ ์ต์ข
์ ์ผ๋ก ๋ฎ์ด์์ฐ๊ธฐ ๋๋ฌธ์ด๋ค. ๊ทธ๋์, age
๋ฅผ ํฌํจํ name
๊ณผ address
๋ชจ๋ ...myInfo
๋ก ์ค์ ์ด ๋๋ค. ๋ฐ๋๋ก Good
์ ์ด๋จ๊น? age
์ name
๋ชจ๋ otherInfo
์์ ์ ์ธํ ๊ฐ์ด ์ ์ฉ๋๋ค. ์๋ํ๋ฉด ...myInfo
๊ฐ ์ ์ผ ๋จผ์ ์ ์ธ๋์๊ธฐ ๋๋ฌธ์ ๊ฒฐ๊ณผ์ ์ผ๋ก ๋ง์ง๋ง์ ์ ์ธํ age
์ name
์ด ์ ์ฉ๋๋ค.
const myInfo = {
age: 28,
name: 'ted',
address: 'Hwaseong',
}
// Bad
const otherInfo = {
age: 25, // This spread always overwrites this property.
...myInfo,
}
๐๐พ otherInfo :>> { age: 28, name: 'ted', address: 'Hwaseong' }โ
// Good
const otherInfo = {
...myInfo,
age: 25,
name: 'jenny',
}
๐๐พ otherInfo :>> { age: 25, name: 'jenny', address: 'Hwaseong' }
Reference
๋๊ธ