λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°

πŸ’¬/γ…γ……γ…Œγ…‹γ…γ…… μ±Œλ¦°μ§€

26일차

🍘

26일차

 

Part 4. TypeScript Essentials

Ch 5. Interfaces

 


 

Ch 5. Interfaces

 

 

μ™ΈλΆ€μ μœΌλ‘œ λ“œλŸ¬λ‚˜λŠ” 객체의 μ‚¬μš© 방식이 μ ν˜€μžˆλŠ” νƒ€μž…

 

interface Person1 {
  name: string;
  age: number;
}

function hello1(person: Person1): void {
  console.log(`μ•ˆλ…•ν•˜μ„Έμš”! ${person.name} μž…λ‹ˆλ‹€.`);
}

const p1: Person1 = {
  name: "Mark",
  age: 39,
};

hello1(p1);

 

 

 πŸ”— optional property 

interface Person3 {
  name: string;
  age?: number; // optional property
  [index: string]: any; // optional property
}

function hello3(person: Person3): void {
  console.log(`μ•ˆλ…•ν•˜μ„Έμš”! ${person.name} μž…λ‹ˆλ‹€.`);
}

const p31: Person3 = {
  name: "Mark",
  age: 39,
};

const p32: Person3 = {
  name: "Anna",
  systers: ["Sung", "Chan"],
};

const p33: Person3 = {
  name: "Bokdaengi",
  father: p31,
  mother: p32,
};

hello3(p33);

 

 πŸ”— Function In Interface 

interface Person4 {
  name: string;
  age: number;
  hello(): void;
}

const p41: Person4 = {
  name: "Mark",
  age: 39,
  hello: function (): void {
    console.log(`μ•ˆλ…•ν•˜μ„Έμš”! ${this.name} μž…λ‹ˆλ‹€.`);
  },
};

const p42: Person4 = {
  name: "Mark",
  age: 39,
  hello(): void {
    console.log(`μ•ˆλ…•ν•˜μ„Έμš”! ${this.name} μž…λ‹ˆλ‹€.`);
  },
};

// thisλ₯Ό μ‚¬μš©ν•˜κΈ° λ•Œλ¬Έμ— 이 μ½”λ“œμ—μ„  적용 λΆˆκ°€
// const p43: Person4 = {
//   name: "Mark",
//   age: 39,
//   hello: (): void => {
//     console.log(`μ•ˆλ…•ν•˜μ„Έμš”! ${this.name} μž…λ‹ˆλ‹€.`);
//   },
// };

p41.hello();
p42.hello();

 

 πŸ”— Class Implements Interface 

interface Iperson1 {
  name: string;
  age?: number;
  hello(): void;
}

class Person implements Iperson1 {
  name: string;
  age?: number | undefined;

  constructor(name: string) {
    this.name = name;
  }

  hello(): void {
    console.log(`μ•ˆλ…•ν•˜μ„Έμš”! ${this.name} μž…λ‹ˆλ‹€.`);
  }
}

const person: Iperson1 = new Person("Mark");
person.hello();

 

 πŸ”— Interface Extends Interface 

interface Iperson2 {
  name: string;
  age?: number;
}

interface IKorean extends Iperson2 {
  city: string;
}

const kL: IKorean = {
  name: "μ΄μ›…μž¬",
  city: "μ„œμšΈ",
};

 

 πŸ”— Function Interface 

interface HelloPerson {
  (name: string, age?: number): void;
}

const helloPerson: HelloPerson = function (name: string) {
  console.log(`μ•ˆλ…•ν•˜μ„Έμš”! ${name} μž…λ‹ˆλ‹€.`);
};

helloPerson("Mark", 39);

 

 πŸ”— Readonly Interface Properties 

interface Person8 {
  name: string;
  age: number;
  readonly gender: string;
}

const p81: Person8 = {
  name: "Mark",
  gender: "male",
};

p81.gender = "female"; // Error λ³€κ²½ λΆˆκ°€ (read only)

 

 πŸ”— Type Alias vs Interface 

// Type Alias
type EatType = (food: string) => void;

// Interface
interface IEat {
  (food: string): void;
}

 

 πŸ”— Declaration Merging - Interface 

interface MergingInterface {
  a: string;
}

interface MergingInterface {
  b: string;
}

let mi: MergingInterface;
mi.a; // a b λ‘˜ λ‹€ κ°€λŠ₯ (Declaration Merging)
mi.b;

 

'πŸ’¬ > γ…γ……γ…Œγ…‹γ…γ…… μ±Œλ¦°μ§€' μΉ΄ν…Œκ³ λ¦¬μ˜ λ‹€λ₯Έ κΈ€

28일차  (0) 2022.03.17
27일차  (0) 2022.03.17
25일차  (0) 2022.03.15
24일차  (0) 2022.03.07
23일차  (0) 2022.02.26