2024-05-29.md
π‘DIL: μ΄νν°λΈ νμ μ€ν¬λ¦½νΈ
μ€ν°λ: μκ° CS, https://github.com/monthly-cs/2024-05-effective-typescript
μμ±μΌ: 2024-05-29
μμ±μ: dusunax
μμ΄ν 32: μ λμ¨μ μΈν°νμ΄μ€λ³΄λ€λ μΈν°νμ΄μ€μ μ λμ¨μ μ¬μ©νκΈ° Prefer Unions of Interfaces to Interfaces with Unions
Unions of Interfaces & Interfaces with Unions
- μ λμ¨ νμ μ μμ±μ κ°μ§λ μΈν°νμ΄μ€
- μΈν°νμ΄μ€μ μ λμ¨ νμ
// π€
// μ λμ¨μ μΈν°νμ΄μ€ Unions of Interfaces
interface User {
id: string;
name: string;
contact: string | number;
}
const user1: User = {
id: "1",
name: "Alice",
contact: "123-456-7890",
};
const user2: User = {
id: "2",
name: "Bob",
contact: 9876543210,
};
// π
// μΈν°νμ΄μ€μ μ λμ¨ Interfaces with Unions
// νμ
κ°λλ₯Ό ν μ μλ€!
interface Cat {
type: "cat";
purrs: boolean;
}
interface Dog {
type: "dog";
barks: boolean;
}
type Animal = Cat | Dog;
const animal1: Animal = {
type: "cat",
purrs: true,
};
const animal2: Animal = {
type: "dog",
barks: true,
};
κ΅μ¬μ μμ
- μ λμ¨μ μΈν°νμ΄μ€: λΌμ΄λΈλ¬λ¦¬μμ μ€λ₯κ° λ°μνκΈ° μ½κ³ , μΈν°νμ΄μ€λ₯Ό λ€λ£¨κΈ° μ΄λ ΅λ€
- λΆλ¦¬λ μΈν°νμ΄μ€λ₯Ό μ λμ¨: μλͺ»λ μ‘°ν©μΌλ‘ μμ΄λ κ²μ λ°©μ§
- νκ·Έ μ¬μ©!
- λ°νμμ νμ μ λ²μλ₯Ό μ’ν μ μλ€ / νμ κ°λ
// π€
interface Layer {
layout: FillLayout | LineLayout | PointLayout;
paint: FillPaint | LinePaint | PointPaint;
}
// π
interface FillLayer {
layout: FillLayout;
paint: FillPaint;
}
interface LineLayer {
layout: LineLayout;
paint: LinePaint;
}
interface PointLayer {
layout: PointLayout;
paint: PointPaint;
}
type Layer = FillLayer | LineLayer | PointLayer;
νκ·Έλ μ λμ¨ ν¨ν΄
- μ μ©ν κ²½μ°
- μΈν°νμ΄μ€μ μμ±μ΄ μ λμ¨μΈ κ²½μ°
- μ¬λ¬ κ°μ μ νμ νλκ° λμμ κ°μ΄ μκ±°λ, λμμ undefinedμΈ κ²½μ°
- μ₯μ
- νμ μ μμ±λ€ κ° κ΄κ³λ₯Ό λͺ¨λΈλ§
- νμ μ€ν¬λ¦½νΈκ° μ½λμ μ νμ±μ 체ν¬νλ λ° λμμ΄ λλ€
// π
interface Person {
name: string;
// νμ
μ λν μ£Όμμ μ’μ§ μλ€
// These will either both be present or not be present
placeOfBirth?: string; // placeOfBirthμ dateOfBirthμ κ΄κ³κ° ννλμ§ μμλ€
dateOfBirth?: Date;
}
// π
interface Person {
name: string;
birth?: {
place: string; // placeμ dateλ ν¨κ» μ‘΄μ¬νλ€
date: Date;
};
}
function eulogize(person: Person) {
console.log(person.name);
const { birth } = person;
if (birth) {
// Person 맀κ°λ³μμμ birthλ§ μ²΄ν¬νλ©΄ λλ€
console.log(`was born on ${birth.date} in ${birth.place}.`);
}
}
β¨ νμ μ ꡬ쑰λ₯Ό μ λ μ μλ μμ: API κ²°κ³Ό
- μΈν°νμ΄μ€λ₯Ό μ λμ¨ν νμ μΌλ‘ κ΄κ³λ₯Ό ννν μ μλ€!~!
interface Name {
name: string;
}
interface PersonWithBirth extends Name {
// νμ₯λ interface
placeOfBirth: string;
dateOfBirth: Date;
}
type Person = Name | PersonWithBirth; // interface μ λμ¨
// βοΈ μ΄λ κ² μΈ μ μλ€!
function eulogize(person: Person) {
if ("placeOfBirth" in person) {
// νκ·Έλ μ λμ¨ μ²΄ν¬
person;
// ^? (parameter) person: PersonWithBirth
const { dateOfBirth } = person; // μ μ
// ^? const dateOfBirth: Date
} else {
// elseλΌλ©΄ Nameμ΄λ€.
// placeOfBirthμ dateOfBirthμ λ λ€ κ°μ§κ³ μμ§ μμ μΈν°νμ΄μ€
}
}
Things to Remember
- Interfaces with multiple properties that are union types are often a mistake because they obscure the relationships between these properties.
- μ λμ¨ νμ μ μμ±μ μ¬λ¬ κ° κ°μ§λ μΈν°νμ΄μ€λ μμ± κ°μ κ΄κ³κ° λΆλͺ νμ§ μλ€.
- Unions of interfaces are more precise and can be understood by TypeScript.
- μ λμ¨μ μΈν°νμ΄μ€λ³΄λ€, μΈν°νμ΄μ€μ μ λμ¨μ΄ λ μ ννκ³ νμ μ€ν¬λ¦½νΈκ° μ΄ν΄νκΈ° μ’λ€
- Use tagged unions to facilitate control flow analysis. Because they are so well supported, this pattern is ubiquitous in TypeScript code.
- νμ μ€ν¬λ¦½νΈκ° μ μ΄ νλ¦μ λΆμν μ μλλ‘, νκ·Έλ₯Ό λ£λ κ²μ κ³ λ €νλ€.
- νκ·Έλ μ λμ¨ ν¨ν΄μ μμ£Ό μ°μ
- Consider whether multiple optional properties could be grouped to more accurately model your data.
- μ΅μ λ μμ±μ΄ λ§μΌλ©΄? κ·Έλ£Ήμ μ§μ΄μ λͺ¨λΈλ§νλ κ²μ κ³ λ €νκΈ°