๐ด 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 |