Design-loving front-end engineer
Ryong
Design-loving front-end engineer
์ „์ฒด ๋ฐฉ๋ฌธ์ž
์˜ค๋Š˜
์–ด์ œ
    • Framework
    • React
      • Concept
      • Library
      • Hook
      • Component
      • Test
    • NodeJS
    • Android
      • Concept
      • Code
      • Sunflower
      • Etc
    • Flutter
      • Concept
      • Package
    • Web
    • Web
    • CSS
    • Language
    • JavaScript
    • TypeScript
    • Kotlin
    • Dart
    • Algorithm
    • Data Structure
    • Programmers
    • Management
    • Git
    • Editor
    • VSCode
    • Knowledge
    • Voice
Design-loving front-end engineer

Ryong

TypeScript

[ TS ] Classes and Interfaces

2022. 7. 17. 16:29

๐Ÿ”ด  Classes

โšช  ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ์˜ ๊ธฐ๋ณธ์ ์ธ ํด๋ž˜์Šค ํ˜•ํƒœ

 
class Player {
  constructor(
    private firstName: string,
    private lastName: string,
    public nickname: string
  ) {}
}

const ryong = new Player("ryong", "jeong", "Ryong");
ryong.nickname;

  โšช  ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์™€๋Š” ๋‹ค๋ฅด๊ฒŒ ์ ‘๊ทผ ์ง€์ •์ž๋ฅผ ์„ ์–ธํ•  ์ˆ˜ ์žˆ๋‹ค.

  โšช  ์ƒ์„ฑ์ž ํŒŒ๋ผ๋ฏธํ„ฐ์— ์žˆ๋Š” ๋ณ€์ˆ˜๋“ค์„ ์ž๋™์œผ๋กœ ํด๋ž˜์Šค์˜ this ๋ณ€์ˆ˜์— ํ• ๋‹นํ•œ๋‹ค.

 

โšช  ์ถ”์ƒ ํด๋ž˜์Šค๋Š” ๋‹ค๋ฅธ ํด๋ž˜์Šค์—์„œ ์ƒ์†๋ฐ›์„ ์ˆ˜๋งŒ ์žˆ๋Š” ํด๋ž˜์Šค์ด๋ฉฐ, ์ƒˆ๋กœ์šด ์ธ์Šคํ„ด์Šค๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์—†๋‹ค.

โšช  ์ถ”์ƒ ๋ฉ”์†Œ๋“œ๋Š” ์ถ”์ƒ ํด๋ž˜์Šค๋ฅผ ์ƒ์†๋ฐ›๋Š” ํด๋ž˜์Šค์—์„œ ๊ตฌํ˜„ํ•ด์•ผ ํ•˜๋Š” ๋ฉ”์†Œ๋“œ๋ฅผ ์˜๋ฏธํ•œ๋‹ค.

 
// ์ถ”์ƒ ํด๋ž˜์Šค
abstract class User {
  constructor(
    protected firstName: string,
    protected lastName: string,
    protected nickname: string
  ) {}
  // ์ถ”์ƒ ๋ฉ”์†Œ๋“œ
  abstract getNickname(): void
}

class Player extends User {
  getNickName() {
    console.log(this.nickname)
  }
}

const ryong = new Player("ryong", "jeong", "Ryong");
ryong.getNickname()  // Ryong
 
type Words = {
  [key: string]: string
  // ex) "food": "kimchi"
}

class Dict {
  private words: Words  // ํ”„๋กœํผํ‹ฐ๋ฅผ ๋งŒ๋“ค๊ณ 
  constructor() {
    this.words = {}  // ์ดˆ๊ธฐํ™”ํ•ด์ฃผ๊ธฐ
  }
  add(word: Word) {  // ๐Ÿ’ก ํด๋ž˜์Šค๋ฅผ ํŒŒ๋ผ๋ฏธํ„ฐ ํƒ€์ž…์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค!
    if (this.words[word.term] === undefined) {
      this.words[word.term] = word.def;
    }
  }
  def(term: string): string {
    return this.words[term]
  }
}

class Word {
  constructor(  // readonly๋ฅผ ํ†ตํ•ด ๋ชจ๋“  ๊ณณ์—์„œ ์ ‘๊ทผํ•˜์ง€๋งŒ ์ˆ˜์ •ํ•  ์ˆœ ์—…๋„๋ก ๋งŒ๋“ฆ
    public readonly term: string,
    public readonly def: string
  ) {}
}

const dist = new Dict()
const kimchi = new Word("kimchi", "ํ•œ๊ตญ์˜ ์Œ์‹");

dict.add(kimchi)
dict.def("kimchi")  // ํ•œ๊ตญ์˜ ์Œ์‹

 

๐ŸŸ   Interfaces

โšช  ์ธํ„ฐํŽ˜์ด์Šค๋Š” ์˜ค๋ธŒ์ ํŠธ์˜ ๋ชจ์–‘์„ ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ์—๊ฒŒ ์„ค๋ช…ํ•ด์ฃผ๊ธฐ ์œ„ํ•ด์„œ ์‚ฌ์šฉ๋œ๋‹ค.

 

โšช  ์ถ”์ƒ ํด๋ž˜์Šค์™€ ์ธํ„ฐํŽ˜์ด์Šค ๋น„๊ต

โšช  ์ถ”์ƒ ํด๋ž˜์Šค

 
abstract class User {
  constructor(
    protected firstName: string,
    protected lastName: string
  ) {}
  abstract fullName(): string
  abstract sayHi(name: string): string
}

class Player extends User {
  fullName() {
    return `${this.firstName} ${this.lastName}`
  }
  sayHi(name: string) {
    return `Hello ${name}. My name is ${this.fullName()}`
  }
}

โšช  ์ธํ„ฐํŽ˜์ด์Šค

 
interface User {
  firstName: string,
  lastName: string,
  fullName(): string,
  sayHi(name: string): string
}
interface Human {
  health: number
}
class Player implements User, Human {
  constructor(
    public firstname: string,
    public lastname: string,
    public health: number
  ) {}
  fullName() {
    return `${this.firstName} ${this.lastName}`
  }
  sayHi(name: string) {
    return `Hello ${name}. My name is ${this.fullName()}`
  }
}
// ์ธํ„ฐํŽ˜์ด์Šค ๋ฆฌํ„ด
function makeUser(user: User): User {
  return {
    firstName: "ryong",
    lastName: "jeong",
    fullName: () => "return string",
    sayHi: (name) => "string"
  }
}
// ์ธํ„ฐํŽ˜์ด์Šค ํŒŒ๋ผ๋ฏธํ„ฐ
makeUser({
  firstName: "ryong",
  lastName: "jeong",
  fullName: () => "return string",
  sayHi: (name) => "string"
})

โšช  ์ถ”์ƒ ํด๋ž˜์Šค๋Š” ์ปดํŒŒ์ผ ์‹œ, ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ์ผ๋ฐ˜ ํด๋ž˜์Šค๋กœ ํ•ด์„๋œ๋‹ค. 

โšช  ๋ฐ˜๋ฉด ์ธํ„ฐํŽ˜์ด์Šค๋Š” ์ถ”์ƒ ํด๋ž˜์Šค์™€ ๊ฐ™์€ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•˜์ง€๋งŒ, ์ปดํŒŒ์ผ ์‹œ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ํŒŒ์ผ์—์„œ ์ œ์™ธ๋˜๋ฏ€๋กœ ํŒŒ์ผ ์‚ฌ์ด์ฆˆ๊ฐ€ ์ค„์–ด๋“ค๊ฒŒ ๋œ๋‹ค.

โšช  ๋”ฐ๋ผ์„œ, ์ถ”์ƒ ํด๋ž˜์Šค๋ฅผ ๋‹ค๋ฅธ ํด๋ž˜์Šค๋“ค์ด ํŠน์ • ๋ชจ์–‘์„ ๋”ฐ๋ฅด๋„๋ก ํ•˜๊ธฐ ์œ„ํ•œ ์šฉ๋„๋กœ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ๊ฐ™์€ ์—ญํ• ์„ ํ•˜๋Š” ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ํŽธ์ด ํšจ์œจ์ ์ด๋‹ค.

โšช  ์ธํ„ฐํŽ˜์ด์Šค๋Š” ์–ด๋Œ‘ํ„ฐ ํŒจํ„ด๊ณผ ๊ฐ™์€ ๋””์ž์ธ ํŒจํ„ด์„ ์‚ฌ์šฉํ•˜์—ฌ ํŒ€๊ณผ ํ•จ๊ป˜ ์ผํ•  ๋•Œ, ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๋งŒ๋“ค์–ด๋‘๊ณ  ํŒ€์›์ด ์›ํ•˜๋Š” ๊ฐ์ž์˜ ๋ฐฉ์‹์œผ๋กœ ํด๋ž˜์Šค๋ฅผ ์ƒ์†ํ•˜๋„๋ก ํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ ๋งŽ์ด ์‚ฌ์šฉ๋œ๋‹ค.

 

โšช  ํƒ€์ž…๊ณผ ์ธํ„ฐํŽ˜์ด์Šค ๋น„๊ต

โšช  ํƒ€์ž…

 
// type์„ ์‚ฌ์šฉํ•˜์—ฌ object ๋ชจ์–‘ ์ •์˜
type PlayerA = {
  name: string
}
type PlayerAA = PlayerA & {
  lastName: string
}
const playerA: PlayerAA = {
  name: "ryong",
  lastname: "jeong"
}

โšช  ์ธํ„ฐํŽ˜์ด์Šค

 
// interface๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ object ๋ชจ์–‘ ์ •์˜
interface PlayerB {
  name: string
}
interface PlayerBB extends PlayerB {
  lastName: string
}
interface PlayerBB {  // ๊ฐ™์€ ์ด๋ฆ„ interface๋ผ๋ฆฌ ์ค‘์ฒฉ ๊ฐ€๋Šฅ
  health: number
}
const playerB: PlayerBB = {
  name: "ryong",
  lastName: "jeong",
  health: 10
}
 
type PlayerA = {
  firstName: string
}
interface PlayerB {
  firstName: string
}
// PlayerA ํƒ€์ž…์„ ์ƒ์†ํ•  ์ˆ˜ ์žˆ์œผ๋ฉด์„œ PlayerB๋„ ์ƒ์†ํ•  ์ˆ˜ ์žˆ๋‹ค.
class User implements PlayerA {
  constructor(
    public firstName: string
  ) {}
}

โšช  ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ ์ปค๋ฎค๋‹ˆํ‹ฐ์—์„œ ํด๋ž˜์Šค๋‚˜ ์˜ค๋ธŒ์ ํŠธ์˜ ๋ชจ์–‘์„ ์ •์˜ํ•˜๊ณ  ์‹ถ์œผ๋ฉด ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์‚ฌ์šฉํ•˜๊ณ , ๋‹ค๋ฅธ ๋ชจ๋“  ๊ฒฝ์šฐ์—์„œ๋Š” ํƒ€์ž…์„ ์“ฐ๋ผ๊ณ  ํ•˜๊ณ  ์žˆ๋‹ค.

 

๐ŸŸก  Polymorphism

 
interface SStorage<T> {
  [key: string]: T
}

class LocalStorage<T> {
  private storage: SStorage<T> = {}  // ์ œ๋„ค๋ฆญ ์ „๋‹ฌ
  set(key: string, value: T) {
    this.storage[key] = value;
  }
  remove(key: string) {
    delete this.storage[key]
  }
  get(key: string): T {
    return this.storage[key]
  }
  clear() {
    this.storage = {}
  }
}

const stringStorage = new LocalStorage<string>();
stringStorage.get("key")
stringStorage.set("hello", "how are you")

const booleanStorage = new LocalStorage<boolean>();
booleanStorage.get("key")
์ €์ž‘์žํ‘œ์‹œ

'TypeScript' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[ TS ] Functions  (0) 2022.07.17
[ TS ] What is TypeScript?  (0) 2022.07.08
    'TypeScript' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€
    • [ TS ] Functions
    • [ TS ] What is TypeScript?
    Design-loving front-end engineer
    Design-loving front-end engineer
    ๋””์ž์ธ์— ๊ด€์‹ฌ์ด ๋งŽ์€ ๋ชจ๋ฐ”์ผ ์•ฑ ์—”์ง€๋‹ˆ์–ด Ryong์ž…๋‹ˆ๋‹ค.

    ํ‹ฐ์Šคํ† ๋ฆฌํˆด๋ฐ”