๋ฌธ์
Promise์ ๊ฐ์ ํ์
์ ๊ฐ์ธ์ธ ํ์
์ด ์์ ๋, ์์ ๊ฐ์ธ์ธ ํ์
์ด ๋ฌด์์ธ์ง ์ด๋ป๊ฒ ์ ์ ์์๊น์?
์์: ๋ค์ด Promise<ExampleType>์ด ์์ ๋, ExampleType์ ์ด๋ป๊ฒ ์ป์ ์ ์์๊น์?
type ExampleType = Promise<string>
type Result = MyAwaited<ExampleType> // string
ํ์ด
type MyAwaited<T> = T extends PromiseLike<infer K> ? MyAwaited<K> : T;
Array vs ArrayLike, Promise vs PromiseLike
ํ์ ์คํฌ๋ฆฝํธ์๋ ArrayLike๋ผ๋๊ฒ ์กด์ฌํ๋ค. Array๋ ์ผ๋ฐ์ ์ธ ๋ฐฐ์ด์ ์๋ฏธํ๋๋ฐ, ArrayLike๋ ๋ฌด์์ผ๊น? ์ด๋ฅผ ์์๋ณด๊ธฐ ์ํด lib.es5.d.ts์ ๊ฐ์ ๊ฐ๊ฐ์ ์คํ์ ์ดํด๋ณด์. Array ArrayLike interface ArrayLike {
yceffort.kr
TypeScript - infer
์กฐ๊ฑด๋ถ ํ์ ์ ์กฐ๊ฑด์์ด ์ฐธ์ผ๋ก ํ๊ฐ๋ ๋์๋ inferํค์๋๋ฅผ ์ฌ์ฉํ ์ ์๋ค. ์๋ฅผ ๋ค์ด,Element<number> extends Element<infer U>์ ๊ฐ์ ํ์ ์ ์์ฑํ๋ฉด, Uํ์ ์ numberํ์ ์ผ๋ก ์ถ๋ก (infer)๋๋ค. ์ดํ, ์ฐธ
velog.io
[TIL] Typescript ์ ๋ค๋ฆญ ๋ฐ ์ ํธ๋ฆฌํฐ ํ์
ํ์ ์คํฌ๋ฆฝํธ ์ ๋ค๋ฆญ ๋ฐ ์ ํธ๋ฆฌํฐ ํ์ ์ ๋ํด ์์๋ณด์
velog.io
์ถ์ฒ
'๐ Typescript > type-challenges' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[TS] type-challenges: 268 - If (0) | 2023.07.19 |
---|---|
[TS] type-challenges: 43. Exclude (0) | 2023.07.14 |
[TS] type-challenges: 18. Length of Tuple (0) | 2023.07.05 |
[TS] type-challenges: 14. First of Array (0) | 2023.07.04 |
[TS] type-challenges : 11. Tuple to Object (0) | 2023.07.03 |