๋ค์ด๊ฐ๊ธฐ์ ์
โข
์ด์ ์๋ ์ข๋ ์ฐจ์ด๊ฐ ๋ช
ํํ์, ~~๋ ์๋ฌ ์ ๋ณด๊ฐ ์๋ฌ๋ค ๋ฑ๋ฑ.
โข
TS ๋ฒ์ ์ด ์
๋ฐ์ดํธ๋๋ฉด ๋ ์๋ก Interface์ Type alias์ฐจ์ด๋ ์ค์ด๋ค๊ณ ์์.
โข
์
๋ฐ์ดํธ๋ฅผ ๊ฑฐ๋ญํ๋ฉฐ ๋ฐ๋๋ ๋ด์ฉ๋ค์ด ๋ง์
Type Alias?
โข
์๋ฐ์คํฌ๋ฆฝํธ ๊ธฐ๋ณธ ํ์
์ธ string, number, booleanโฆ
โข
type ํค์๋๋ก ๊ฐ๋ฐ์๊ฐ ์๋ก์ด ํ์
์ ์์ฑ ํ ์ ์์.
์ ์ธ ๋ณํฉ(Declaration Merging)
Interface 
interface InterfaceName {
property1: string
}
interface InterfaceName {
property2: string
}
TypeScript
๋ณต์ฌ
โข
๊ฐ์ ์ด๋ฆ์ interface๋ฅผ ๋๋ฒ ์ ์ธํ ๊ฒฝ์ฐ ์๋์ผ๋ก ๋ณํฉ ๋์ด ๊ฒฐ๊ณผ์ ์ผ๋ก InterfaceName์ property1, property2๋ฅผ ๋ค ๊ฐ์ง๊ฒ ๋จ
Type 
type TypeName ={
property1: string
}
// ์๋ฌ -> ๊ฐ์์ด๋ฆ์ ํ์
์ ์ธ ๋ถ๊ฐ๋ฅ
type TypeName ={
property2: string
}
TypeScript
๋ณต์ฌ
โข
์ธํฐํ์ด์ค์๋ ๋ค๋ฅด๊ฒ ๊ฐ์์ด๋ฆ์ผ๋ก ์ ์ธํ๋ค๊ณ ์๋์ผ๋ก ํ์
์ด ํ์ฅ๋์ง ์์
๋ช ์์ ๋ณํฉ
Interface 
interface Interface1 {
property1: string
}
interface Interface2 extends Interface {
property2: string
}
TypeScript
๋ณต์ฌ
โข
extends๋ฅผ ์ด์ฉํด ๋ค๋ฅธ ์ธํฐํ์ด์ค๋ฅผ ์์ ๋ฐ์ ์ ์์ - ํด๋์ค๋ ๊ฐ๋ฅ
Type alias 
// Interface1, Interface2
type Type1 = { property1: string }
type Type2 = { property2: string }
type NewType = Type1 & Type2;
type AnotherNewType = Interface1 & Interface2
TypeScript
๋ณต์ฌ
โข
Intersection - &๋ฅผ ์ด์ฉํ์ฌ ๋๊ฐ์ ํ์
์ ์ด์ฉํด ํ๋์ ํ์
์ผ๋ก ์์ฑ ๊ฐ๋ฅ
โข
์ด๋ฏธ ์ ์ธ๋ ์ธํฐํ์ด์ค๋ฅผ ์ด์ฉํ์ฌ Intersection ๊ฐ๋ฅ โ Type alias๋ฅผ ์ด์ฉํ์ฌ ์ธํฐํ์ด์ค์ Interserction์ ๋น์ฐํ ๋ถ๊ฐ๋ฅ(์ง์ ์ํ๋๊น..)
Union
Interface 
โข
์ธํฐํ์ด์ค๋ union ๋ถ๊ฐ โ ์ธํฐํ์ด์ค๋ฅผ ๋ฌถ์ด union์ ๊ฐ๋ฅ
Type alias 
// NewType์ string ๋๋ number๊ฐ ๋ ์ ์์
type NewType = string | number;
interface Interface1 {
baseProperty: number;
property1: string;
}
interface Interface2 extends Interface {
baseProperty: string;
property2: number;
}
// AnotherNewType์ ๊ฒฝ์ฐ
type AnotherNewType = Interface1 | Interface2
TypeScript
๋ณต์ฌ
โข
A ๋๋ B ํ์
์ผ ์ ์๋ค ์ ๋ํ ์ ์
โข
interface ๋ํ union type์ ์์ฑ ๊ฐ๋ฅํ์ง๋ง ๊ธฐ๋ณธ์ ์ผ๋ก ์ฝ๋์์๋ ๊ณตํต๋ ํ์
์ผ๋ก๋ง ์ถ๋ก ๋จ
โข
์ ์ฝ๋์ AnotherNewType์ ๊ฒฝ์ฐ Type Narrow๋ฅผ ์ฌ์ฉํ์ง ์์ ๊ฒฝ์ฐ baseProperty๋ง ์ ๊ทผ๊ฐ๋ฅ โ TypeNarrow๋ก ์ ๊ทผํด์ผํจ
๊ฒฐ๋ก
TS ๊ณต์ ๋ฌธ์์ ์ถ์ฒ
โข
๊ณต์ ๋ฌธ์์์๋ Interface๋ฅผ ์ฃผ๋ก ์ฌ์ฉํ๊ณ type์ด ํ์ ํ ๋๋ง type ์ฌ์ฉ์ ์ถ์ฒํจ โ ๋ณธ์ธ๊ณผ ๊ฐ์ ๊ฒฝ์ฐ์๋ Union Type์ ์ธ์ผ์ด ๋ง์์
์ฑ๋ฅ
โข
์ธํฐํ์ด์ค๋ ์บ์๋์ด ์ฑ๋ฅ์ด ๋๋น ๋ฆ