jaenny.dev
article thumbnail
[타입챌린지/type-challenge] Awaited
Frontend/Type-Challenge 2023. 1. 25. 21:14

문제 Promise와 같은 타입에 감싸진 타입이 있을 때, Promise 내부에 감싸진 타입이 무엇인지 어떻게 알 수 있을까요? 예를 들어, Promise이 있을 때 ExampleType을 어떻게 얻을 수 있을까요? type ExampleType = Promise type Result = MyAwaited // string 풀이 먼저 error를 내야하는 케이스를 고려해줍니다. 제네릭 타입으로는 Promise에 감싸진 타입 혹은, then을 메서드로 가지는 Promise와 같은 타입을 받아야 합니다. 이 경우가 아니라면 error를 내도록 Promise를 extends하도록 제네릭 T를 제한해줍니다. 그 다음 Promise 내부의 타입에 접근합니다. infer를 사용해줍니다. 먼저 T extends Pr..

-Like 타입은 뭘까? Array vs. ArrayLike / Promise vs. PromiseLike
Frontend 2023. 1. 24. 16:05

타입스크립트에는 -Like 타입이 있다. Array와 ArrayLike, Promise와 PromiseLike -Like postFix가 붙여진 타입이 왜 필요한지 알아보자. Array vs. ArrayLike Array lib.es2015.core.ts 파일에서는 Array 타입을 다음과 같이 정의하고 있다. 자주 사용하는 find, fill 등의 메소드도 찾아볼 수 있다. interface Array { /** * Returns the value of the first element in the array where predicate is true, and undefined * otherwise. * @param predicate find calls predicate once for each elem..