๐Ÿ’™ Type Challenges/type-challenges

[TS] type-challenges: 189 - Awaited

์„ ๋‹ฌ 2023. 7. 17. 20:15
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ

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

 

์ถœ์ฒ˜

https://github.com/type-challenges/type-challenges/blob/main/questions/00189-easy-awaited/README.ko.md

 

๋ฐ˜์‘ํ˜•

'๐Ÿ’™ Type Challenges > 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