개발/TypeScript

    11. 타입스크립트(TypeScript) - Polymorphism + Generic + Class + interface

    다형성은 다른 모양의 코드를 가질 수 있게 해주는 것 다형성을 이룰 수 있는 방법은 Generic을 사용하는 것 Generic은 Placeholder 타입을 쓸 수 있도록 해준다 (Concrete type이 아니라 Placeholder type) 같은 코드를 다른 타입에 대해서 쓸 수 있도록 해준다 → 다른 타입에게 물려줄 수 있다 예제 - 브라우저에서 쓰는 로컬 스토리지 만들기 interface SStorage { [key: string]: T } class LocalStorage { private storage: SStorage = {} set(key: string, value: T) { this.storage[key] = value; } remove(key: string) { delete this...

    10. 타입스크립트(TypeScript) - Interface

    Interface 주로 다른 누군가가 데이터를 덮어쓰는 것을 방지하기 위해 private이나 protected property를 사용 타입을 concrete 방법이 아니라 특정 값 중 하나만 갖도록 설정할 수도 있다 type Team = "red" | "blue" | "yellow" type Health = 1 | 5 | 10 type Player = { nickname: string, team: Team, health : Health } const a: Player = { nickname: "hello", team: "red", health: 10 } Interface : 오브젝트의 모양을 설명하는 다른 방법 type Team = "red" | "blue" | "yellow" type Health = ..

    9. 타입스크립트(TypeScript) - Class

    Class → 파라미터들을 써주기만 하면, TS가 알아서 Constructor 함수를 만들어 준다 class Player = { Constructor( private firstName: string, private lastName: string, public nickname: string ) {} } const a = new Player("hello", "bye", "thisisnickname"); a.firstName; // 이 부분은 작동하지 않는다. 왜냐하면 firstName이 private이기 때문이다. 추상 클래스(Abstract Class) TS와 객체지향 프로그램이 가지고 있는 뛰어난 기능 중 하나는 추상 클래스(Abstract Class)이다 추상 클래스란 다른 곳에서 상속받을 수만 있는 ..

    8. 타입스크립트(TypeScript) - Concrete Type & Generic Type

    1. Concrete Type → 우리가 전부터 봐왔던 타입을 의미 → number, boolean, string 2. Generic Type → 타입의 Placeholder 같은 것 → Concrete type을 사용하는 대신 쓸 수 있음 → Call Signature를 작성할 때, 들어올 확실한 타입을 모를 때 generic을 사용한다. 즉, TS가 타입을 추론하게 할 수 있다. TS에 generic을 사용하고 싶다고 알려준다 를 사용하여 generic 이름을 붙인다 → 이름은 T 또는 V를 많이 사용한다 타입을 generic 이름으로 바꾼다 type SuperPrint = { (arr: GenericName[]): void // 이름은 마음대로 설정할 수 있다 } type SuperPrint2 = ..