Skip to Content
Sunbeen's Blog
Documents๊ตฌ TypescriptBasic_Grammer04. ๐ŸŸฆ TypeScript - type, interface, union, intersection ์ •๋ฆฌ

04. ๐ŸŸฆ TypeScript - type, interface, union, intersection ์ •๋ฆฌ

๐Ÿ“Œ 1. type vs interface

๐Ÿ“Œ type vs interface ์ฐจ์ด ์ •๋ฆฌ

ํ•ญ๋ชฉtypeinterface
์„ ์–ธ ๋ฐฉ์‹type A = { a: string }interface A { a: string }
์‚ฌ์šฉ ๋Œ€์ƒ๋ชจ๋“  ํƒ€์ž… ๊ฐ€๋Šฅ (๊ธฐ๋ณธ, ์œ ๋‹ˆ์˜จ, ํŠœํ”Œ ๋“ฑ)๊ฐ์ฒด ๊ตฌ์กฐ(Shape) ์ •์˜์— ํŠนํ™”
ํ™•์žฅ ๋ฐฉ์‹& ์—ฐ์‚ฐ์ž๋กœ ํƒ€์ž… ํ™•์žฅextends ํ‚ค์›Œ๋“œ๋กœ ์ƒ์†
์ค‘๋ณต ์„ ์–ธโŒ ์ค‘๋ณต ์„ ์–ธ ์‹œ ์—๋Ÿฌ โœ…์ค‘๋ณต ์„ ์–ธ ์‹œ ์ž๋™ ๋ณ‘ํ•ฉ(Merge)
์œ ์—ฐ์„ฑ๋” ์œ ์—ฐํ•˜๊ณ  ๋ณต์žกํ•œ ํƒ€์ž… ํ‘œํ˜„ ๊ฐ€๋Šฅ๊ตฌ์กฐ์  ์„ค๊ณ„์— ์ตœ์ ํ™”
ํด๋ž˜์Šค ๊ตฌํ˜„๐Ÿšซ ์ง์ ‘ ๊ตฌํ˜„ ๋ถˆ๊ฐ€โœ… implements๋กœ ํด๋ž˜์Šค์— ์ ์šฉ ๊ฐ€๋Šฅ
์ œ๋„ค๋ฆญโœ… ๊ฐ€๋Šฅโœ… ๊ฐ€๋Šฅ
type TypeA = { a: string }; const a1: TypeA = { a: "hello" }; interface InterfaceB { a: string; } const b1: InterfaceB = { a: "hi" };

๐Ÿ” ํ™•์žฅ

// type ํ™•์žฅ type Base = { x: number }; type Extended = Base & { y: number }; const obj: Extended = { x: 1, y: 2 }; // interface ํ™•์žฅ interface IBase { x: number; } interface IExtended extends IBase { y: number; } interface IBase { z: number; } const obj2: IExtended = { x: 3, y: 4, z: 5 };

๐Ÿงฉ ํด๋ž˜์Šค ๊ตฌํ˜„

interface Person { name: string; } class Human implements Person { name = "John"; }

๐Ÿ“Œ 2. ์ œ๋„ค๋ฆญ ์‚ฌ์šฉ ๊ฐ€๋Šฅ

type Box<T> = { value: T }; interface IBox<T> { value: T; } const tBox: Box<string> = { value: "hello" }; const iBox: IBox<number> = { value: 42 };

๐Ÿ“Œ 3. ์œ ๋‹ˆ์˜จ ํƒ€์ž… (|)

์—ฌ๋Ÿฌ ํƒ€์ž… ์ค‘ ํ•˜๋‚˜๋งŒ ๋งŒ์กฑํ•˜๋ฉด ๋˜๋Š” ๊ตฌ์กฐ

๊ณ„์‚ฐ์ด ํ˜ผ๋ž€์Šค๋Ÿฌ์šธ ์ˆ˜ ์žˆ์Œ

ํƒ€์ž… ์Šคํฌ๋ฆฝํŠธ๋Š” ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ์ „๋ถ€ ๊ณ ๋ คํ•œ๋‹ค,

2 _ 2 _ 2 ์ด 8๊ฐ€์ง€์˜ ๋ชจ๋“  ๊ฐ€์ง“์ˆ˜๋ฅผ ๊ณ ๋ คํ•œ๋‹ค.

ํƒ€์ž…์˜ ๊ณ ๋ ค๊ฐ€ ์ „๋ถ€ ๊ผฌ์—ฌ๋ฒ„๋ฆฌ๊ธฐ ๋•Œ๋ฌธ์—, ์ž˜๋ชป๋œ ์ƒํ™ฉ์ด ๋‚˜์˜ฌ ์ˆ˜ ์žˆ๋‹ค.

function add(x: string | number, y: string | number): string | number { return x + y; } const result: string = add(1, 2) as string; // ํƒ€์ž… ํ˜ผ๋™ ๊ฐ€๋Šฅ
type A = { hello: "world" } | { sunbin: "choi" }; const u1: A = { hello: "world" }; const u2: A = { sunbin: "choi" }; // const u3: A = { hello: "world", sunbin: "choi" } // โŒ ์˜ค๋ฅ˜

๐Ÿ“Œ 4. ์ธํ„ฐ์„น์…˜ ํƒ€์ž… (&)

intersection

๋ชจ๋“  ํƒ€์ž…์„ ๋™์‹œ์— ๋งŒ์กฑํ•ด์•ผ ํ•จ

์†์„ฑ ๋ณ‘ํ•ฉ์ด ์ผ์–ด๋‚จ

type A = { hello: "world" } & { sunbin: "choi" }; const a: A = { hello: "world", sunbin: "choi" };

๐Ÿ“Œ 5. ์‹œ๊ฐ์ ์œผ๋กœ ๋น„๊ต

  • Union (|)
  • Intersection (&) ๋ชจ๋“  ํƒ€์ž…์„ ๋™์‹œ์— ๋งŒ์กฑํ•ด์•ผ ํ•จ

๐Ÿ“Œ 6. ์ •๋ฆฌ ์š”์•ฝ ์ฝ”๋“œ

// type type T = { a: string } & { b: number }; const t: T = { a: "hi", b: 123 }; // interface interface I1 { a: string; } interface I2 extends I1 { b: number; } const i: I2 = { a: "hi", b: 123 }; // union type U = { x: string } | { y: number }; const u: U = { x: "ok" }; // intersection type I = { x: string } & { y: number }; const x: I = { x: "ok", y: 1 };

๐Ÿ“Œ 7. Type Alias (ํƒ€์ž… ๋ณ„์นญ) ํ™•์žฅ

  • type์€ ๊ฐ์ฒด ํ˜•ํƒœ๋ฟ ์•„๋‹ˆ๋ผ ์œ ๋‹ˆ์˜จ, ํŠœํ”Œ, ๊ธฐ๋ณธ ํƒ€์ž… ๋“ฑ ๋‹ค์–‘ํ•œ ํƒ€์ž… ํ‘œํ˜„์ด ๊ฐ€๋Šฅ
  • & (Intersection)์„ ์‚ฌ์šฉํ•˜์—ฌ ํƒ€์ž…์„ ํ™•์žฅํ•จ
  • ์†์„ฑ์ด ๋ณ‘ํ•ฉ๋˜์–ด ๋ชจ๋‘ ํฌํ•จํ•ด์•ผ ํ•จ
type Animal = { breath: true }; type Poyouryu = Animal & { breed: true }; type Human = Poyouryu & { think: true }; const sunbin: Human = { breath: true, breed: true, think: true };

๐Ÿ“Œ 8. Interface ํ™•์žฅ (extends)

  • ๊ฐ์ฒด ํ˜•ํƒœ๋ฅผ ์ •์˜ํ•˜๋Š” ๋ฐ ํŠนํ™”๋œ ๋ฌธ๋ฒ•
  • extends ํ‚ค์›Œ๋“œ๋ฅผ ํ†ตํ•ด ๊ณ„์ธต์ ์œผ๋กœ ํ™•์žฅ ๊ฐ€๋Šฅ
  • ์ƒ์†๋ฐ›์€ ์†์„ฑ์„ ์ž๋™ ํฌํ•จํ•จ
interface A { breath: true; } interface B extends A { breed: true; } const b: B = { breath: true, breed: true };
  • โœ… ํƒ€์ž… ๋ณ„์นญ์„ ์ธํ„ฐํŽ˜์ด์Šค์—์„œ ํ™•์žฅํ•˜๋Š” ๊ฒƒ๋„ ๊ฐ€๋Šฅ
type Poyouryu = { breath: true; breed: true }; interface C extends Poyouryu { think: true; } const c: C = { breath: true, breed: true, think: true };
  • โœ”๏ธ interface โ†’ type ํ™•์žฅ์€ ๋ถˆ๊ฐ€
  • โ— type โ†’ interface ํ™•์žฅ์€ ๊ฐ€๋Šฅ

๐Ÿ“Œ 9. Interface ์ค‘๋ณต ์„ ์–ธ (Declaration Merging)

  • interface๋Š” ๋™์ผํ•œ ์ด๋ฆ„์œผ๋กœ ์—ฌ๋Ÿฌ ๋ฒˆ ์„ ์–ธ ์‹œ ๋ณ‘ํ•ฉ๋จ
  • ์ด๋ฅผ ํ†ตํ•ด ์™ธ๋ถ€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ˆ˜์ •ํ•˜์ง€ ์•Š๊ณ  ํ™•์žฅํ•  ์ˆ˜ ์žˆ์Œ
  • ์‹ค๋ฌด์—์„œ Window, HTMLElement ๋“ฑ์„ ํ™•์žฅํ•  ๋•Œ ์ž์ฃผ ์‚ฌ์šฉ
interface A { talk: () => void; } interface A { eat: () => void; } interface A { shit: () => void; } const a: A = { talk: () => {}, eat: () => {}, shit: () => {} }; // ์‹ค์ „ ์˜ˆ์ œ HTMLElement์— ์ปค์Šคํ…€ ์†์„ฑ ์ถ”๊ฐ€ interface HTMLElement { myCustomValue?: number; } const div = document.createElement("div"); div.myCustomValue = 42; // โœ… ํƒ€์ž… ์ธ์‹๋จ // ์ปค์Šคํ„ฐ๋งˆ์ด์ง•: ์ธํ„ฐํŽ˜์ด์Šค ๋ณ‘ํ•ฉ // axios์—์„œ config, response ๋“ฑ์— ์ปค์Šคํ…€ ํ•„๋“œ๋ฅผ ์ถ”๊ฐ€ํ•˜์—ฌ ์‹ค๋ฌด์—์„œ ํ™œ์šฉ // ์ฃผ๋กœ RequestConfig, AxiosResponse ํ™•์žฅ์— ์‚ฌ์šฉ๋จ import axios, { AxiosRequestConfig, AxiosResponse } from "axios"; declare module "axios" { export interface AxiosRequestConfig { authToken?: string; } export interface AxiosResponse<T = any> { requestId?: string; } } axios .get("/api/test", { authToken: "abc123" // โœ… ํƒ€์ž… ์ธ์‹๋จ }) .then((res) => { console.log(res.requestId); // โœ… ์ปค์Šคํ…€ ํ•„๋“œ ์‚ฌ์šฉ ๊ฐ€๋Šฅ });
  • โœ… ํƒ€์ž… ๋ณ‘ํ•ฉ์€ interface๋งŒ ๊ฐ€๋Šฅ
  • โŒ type์€ ๋™์ผ ์ด๋ฆ„์œผ๋กœ ์ค‘๋ณต ์„ ์–ธ ๋ถˆ๊ฐ€

๐Ÿ“Œ 10. ๋„ค์ด๋ฐ ๊ทœ์น™ (์ปจ๋ฒค์…˜)

์šฉ๋„๋„ค์ด๋ฐ ์˜ˆ์‹œ์„ค๋ช…
interfaceIPropsI ์ ‘๋‘์–ด๋ฅผ ๋ถ™์ด๋Š” ํ—๊ฐ€๋ฆฌ์•ˆ ํ‘œ๊ธฐ๋ฒ• ์Šคํƒ€์ผ (์˜ˆ์ „ ์Šคํƒ€์ผ)
type aliasTTypeT ์ ‘๋‘์–ด (Type์ž„์„ ๋ช…ํ™•ํžˆ ํ•˜๋ ค๋Š” ์˜๋„)
enumEKindE ์ ‘๋‘์–ด (Enum์ž„์„ ๊ตฌ๋ถ„ํ•˜๊ธฐ ์œ„ํ•จ)
modern ๋ฐฉ์‹Props, Kindโ— ์š”์ฆ˜์€ ์ ‘๋‘์–ด ์—†์ด ๋‹จ์ˆœํ•˜๊ฒŒ ์“ฐ๋Š” ๊ฒฝํ–ฅ ๋งŽ์Œ
Last updated on