TIL 22.08.28 | Map 객체, flatMap 메서드, Generic 과 Any

HyeonWooGa·2022년 8월 28일
0

TIL

목록 보기
22/39
  1. Map 객체 (JavaScript)
    • 키값으로 다양한 자료형을 쓸 수 있는 객체
    • set(), get(), delete(), keys(), values(), entries(), clear() 등 다양한 메서드 사용하여 간편함

  2. Array.prototype.flatMap() 메서드 (JavaScript)
    • Array.prototype.flat() 이 뒤따르는 Array.prototype.map() 과 동일하게 작동합니다.

  3. Generic 과 any 차이, 복수 Generic (TypeScript)

// 그렇다면 그냥 any를 넣는 것과 Generic의 차이는 무엇일까?

type SuperPrint = {
(arr: any[]): any
}

const superPrint: SuperPrint = (arr) => arr[0]

let a = superPrint([1, "b", true]);
// pass
a.toUpperCase();

// any를 사용하면 위와 같은 경우에도 에러가 발생하지 않는다

type SuperPrint = {
(arr: T[]): T
}

const superPrint: SuperPrint = (arr) => arr[0]

let a = superPrint([1, "b", true]);
// error
a.toUpperCase();

// Generic의 경우 에러가 발생해 보호받을 수 있다
//// Call Signature를 concrete type으로 하나씩 추가하는 형태이기 때문!

type SuperPrint = {
(arr: T[], x: M): T
}

const superPrint: SuperPrint = (arr, x) => arr[0]

let a = superPrint([1, "b", true], "hi");

// 위와 같이 복수의 Generic을 선언해 사용할 수 있다
profile
Aim for the TOP, Developer

0개의 댓글