-
[TypeScript] TypeScript로 블록체인 만들기 - day8,9TypeScript 2023. 2. 14. 15:38반응형
이전 챕터 보러가기
https://valiafern-0205.tistory.com/3
[TypeScript] TypeScript로 블록체인 만들기 - day7
이전 강의 듣기 https://valiafern-0205.tistory.com/2 // 값을 특정값으로 제한(string,red,blue,yellow) type Team = "red" | "blue" | "yellow" // 값을 특정값으로 제한(number,1,5,10) type Health = 1 | 5 | 10 type Player = { nickname : str
valiafern-0205.tistory.com
Polymorphism(다향성)
Polymorphism(다형성), Generic(제네릭), Class(클래스), Interface(인터페이스)를 합쳐 보자.
Polymorphism은 Generic(Placeholder. not concrete)를 사용해서 구현.
다른 모양의 코드를 가질 수 있게 해준다.
TS가 placeholder 타입을 concrete 타입으로 바꿔준다.
같은 코드를 다른 타입에서 쓸 수 있게 해준다.
브라우저에서 쓰는 로컬 스토리지 API와 비슷한 API를 가지고 클래스를 만들어 보기(일반적으로 js에서 쓰는)
// 기존에 Storage가 정의가 되어 있다 -> SStorage interface SStorage<T> { [key:string] : T } // 1. 제네릭을 클래스로 보냄 // 2. 클래스는 제네릭을 인터페이스로 보냄 // 3. 인페이스는 제니릭을 사용 class LocalStorage<T> { private storage : SStorage<T> = {} set(key:string, value:T){ // API 디자인 구현 this.storage[key] = value; } remove(key:string){ delete this.storage[key] } get(key:string) : T { return this.storage[key] } clear(){ this.storage = {} } } const stringsStorage = new LocalStorage<string>() stringsStorage.get("ket") stringsStorage.set("hello","how are u?") const booleanStorage = new LocalStorage<boolean>() booleanStorage.get("xxx"); booleanStorage.set("hello", true)
배운점
다형성을 연습을 많이 해야될 거 같아요
여태까지 배운거를 좋합으로 해서 문제를 푸는게 이해가 되지를 않는다
다음 챕터 보러가기
https://valiafern-0205.tistory.com/5
[TypeScript] 미니 API 만들어 보기
타입스크립트 노XX 타입스크립트 챌린지를 하면서 배운 내뇽을 정리 하였습니다. 타입스크립트로 JS에서 쓰는 API를 한번 Typescript로 만들어 보았습니다.. 만든 API는 총 2개입니다 첫번째, LocalStorag
valiafern-0205.tistory.com
반응형'TypeScript' 카테고리의 다른 글
[TypeScript] TypeScript로 블록체인 만들기 - day10 (0) 2023.02.16 [TypeScript] 미니 API 만들어 보기 (0) 2023.02.15 [TypeScript] TypeScript로 블록체인 만들기 - day7 (0) 2023.02.13 [TypeScript] 사전 클래스 예제 풀기 (0) 2023.02.11