TypeScript의 핡심 원칙 쀑 ν•˜λ‚˜λŠ” νƒ€μž… 검사가 κ°’μ˜ ν˜•νƒœμ— μ΄ˆμ μ„ λ§žμΆ”κ³  μžˆλ‹€λŠ” 것인데, TypeScriptμ—μ„œ, μΈν„°νŽ˜μ΄μŠ€(interface)λŠ”

  • 이런 νƒ€μž…λ“€μ˜ 이름을 μ§“λŠ” 역할을 ν•˜κ³ 
  • μ½”λ“œ μ•ˆμ˜ 계약을 μ •μ˜ν•˜λŠ” κ²ƒλΏλ§Œ μ•„λ‹ˆλΌ
  • ν”„λ‘œμ νŠΈ μ™ΈλΆ€μ—μ„œ μ‚¬μš©ν•˜λŠ” μ½”λ“œμ˜ 계약을 μ •μ˜ν•˜λŠ” κ°•λ ₯ν•œ 방법이닀.



첫 번째 μΈν„°νŽ˜μ΄μŠ€ (Our First Interface)

μ–΄λ–»κ²Œ μΈν„°νŽ˜μ΄μŠ€κ°€ λ™μž‘ν•˜λŠ”μ§€ ν™•μΈν•˜λŠ” κ°€μž₯ μ‰¬μš΄ 방법은 κ°„λ‹¨ν•œ 예제둜 ν™•μΈν•˜κΈ°!

// interface μ‚¬μš© μ „
// printLabel ν•¨μˆ˜λŠ” string νƒ€μž… label을 κ°–λŠ” 객체λ₯Ό ν•˜λ‚˜μ˜ λ§€κ°œλ³€μˆ˜λ₯Ό κ°–λŠ”λ‹€
function printLabel(labeledObj: { label: string }) {
  console.log(labeledObj.label);
}

let myObj = { size: 10, label: "Size 10 Object" };
printLabel(myObj);

// interface μ‚¬μš© ν›„
// λ¬Έμžμ—΄ νƒ€μž…μ˜ ν”„λ‘œνΌν‹° label을 가진 μΈν„°νŽ˜μ΄μŠ€λ‘œ μž‘μ„±
interface LabeledValue {
  label: string;
}

function printLabel(labeledObj: LabeledValue) {
  console.log(labeledObj.label);
}

let myObj = { size: 10, label: "Size 10 Object" };
printLabel(myObj);


Optional Properties

μΈν„°νŽ˜μ΄μŠ€μ˜ λͺ¨λ“  ν”„λ‘œνΌν‹°κ°€ ν•„μš”ν•œ 것은 μ•„λ‹ˆλ‹€. μ–΄λ–€ μ‘°κ±΄μ—μ„œλ§Œ μ‘΄μž¬ν•˜κ±°λ‚˜ μ•„μ˜ˆ 없을 μˆ˜λ„ μžˆλ‹€.

선택적 ν”„λ‘œνΌν‹°λ“€μ€ 객체 μ•ˆμ˜ λͺ‡ 개의 ν”„λ‘œνΌν‹°λ§Œ μ±„μ›Œ ν•¨μˆ˜μ— μ „λ‹¬ν•˜λŠ” β€œoption bags” 같은 νŒ¨ν„΄μ„ λ§Œλ“€ λ•Œ μœ μš©ν•˜λ‹€!

interface PaintOptions {
  shape: Shape;
  xPos?: number;
  yPos?: number;
}

function paintShape(opts: PaintOptions) {
  // ...
}

const shape = getShape();
paintShape({ shape });
paintShape({ shape, xPos: 100 });
paintShape({ shape, yPos: 100 });
paintShape({ shape, xPos: 100, yPos: 100 });



Readonly properties

일뢀 ν”„λ‘œνΌν‹°λ“€μ€ 객체가 처음 생성될 λ•Œλ§Œ μˆ˜μ • κ°€λŠ₯ν•΄μ•Ό ν•  λ•Œ μ‚¬μš©.

interface Point {
  readonly x: number;
  readonly y: number;
}

let p1: Point = { x: 10, y: 20 };
p1.x = 5; // 였λ₯˜!



인덱슀 νƒ€μž… (Index Types)

μΈν„°νŽ˜μ΄μŠ€μ—μ„œ 속성 이름을 ꡬ체적으둜 μ •μ˜ν•˜μ§€ μ•Šκ³  κ°’μ˜ νƒ€μž…λ§Œ μ •μ˜ν•˜λŠ” 것을 인덱슀 νƒ€μž…μ΄λΌ ν•œλ‹€.

interface SquareConfig {
  color?: string;
  width?: number;
  [propName: string]: any;
}

let squareOptions = { color: "red", width: 100 };
let mySquare = createSquare(squareOptions);



Extending Types

클래슀처럼, μΈν„°νŽ˜μ΄μŠ€λ“€λ„ ν™•μž₯(extend)이 κ°€λŠ₯ν•˜λ‹€.

ν™•μž₯(extend)은 ν•œ μΈν„°νŽ˜μ΄μŠ€μ˜ 멀버λ₯Ό λ‹€λ₯Έ μΈν„°νŽ˜μ΄μŠ€μ— λ³΅μ‚¬ν•˜λŠ” 것을 κ°€λŠ₯ν•˜κ²Œ ν•΄μ£ΌλŠ”λ°, μž¬μ‚¬μš©μ„± 높은 μ»΄ν¬λ„ŒνŠΈλ‘œ μͺΌκ°€ λ•Œ μœ μ—°ν•˜κ²Œ ν•  수 μžˆλ‹€.

// 예제 1
interface BasicAddress {
  name?: string;
  street: string;
  city: string;
  country: string;
  postalCode: string;
}

interface AddressWithUnit extends BasicAddress {
  unit: string;
}

// 예제 2
interface Colorful {
  color: string;
}

interface Circle {
  radius: number;
}

interface ColorfulCircle extends Colorful, Circle {}

const cc: ColorfulCircle = {
  color: "red",
  radius: 42,
};



ꡐ차 νƒ€μž… (Intersection Types)

interfacesλ₯Ό μ‚¬μš©ν•˜λ©΄ λ‹€λ₯Έ μœ ν˜•μ„ ν™•μž₯ν•˜μ—¬ λ‹€λ₯Έ μœ ν˜•μ—μ„œ μƒˆ μœ ν˜•μ„ ꡬ좕할 수 μžˆλ‹€. Intersection Types은 &μ—°μ‚°μžλ₯Ό μ‚¬μš©ν•˜μ—¬ μ •μ˜ν•œλ‹€.

interface Colorful {
  color: string;
}
interface Circle {
  radius: number;
}

type ColorfulCircle = Colorful & Circle;



참고 자료: typescript-kr.github.io