04. ๐ฆ TypeScript - type, interface, union, intersection ์ ๋ฆฌ
๐ 1. type vs interface
๐ type vs interface ์ฐจ์ด ์ ๋ฆฌ
ํญ๋ชฉ | type | interface |
---|---|---|
์ ์ธ ๋ฐฉ์ | 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. ๋ค์ด๋ฐ ๊ท์น (์ปจ๋ฒค์ )
์ฉ๋ | ๋ค์ด๋ฐ ์์ | ์ค๋ช |
---|---|---|
interface | IProps | I ์ ๋์ด๋ฅผ ๋ถ์ด๋ ํ๊ฐ๋ฆฌ์ ํ๊ธฐ๋ฒ ์คํ์ผ (์์ ์คํ์ผ) |
type alias | TType | T ์ ๋์ด (Type์์ ๋ช ํํ ํ๋ ค๋ ์๋) |
enum | EKind | E ์ ๋์ด (Enum์์ ๊ตฌ๋ถํ๊ธฐ ์ํจ) |
modern ๋ฐฉ์ | Props, Kind | โ ์์ฆ์ ์ ๋์ด ์์ด ๋จ์ํ๊ฒ ์ฐ๋ ๊ฒฝํฅ ๋ง์ |
Last updated on