-
Notifications
You must be signed in to change notification settings - Fork 0
/
00189-easy-awaited.ts
54 lines (40 loc) · 1.5 KB
/
00189-easy-awaited.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/*
189 - Awaited
-------
by Maciej Sikora (@maciejsikora) #easy #promise #built-in
### Question
If we have a type which is wrapped type like Promise. How we can get a type which is inside the wrapped type?
For example: if we have `Promise<ExampleType>` how to get ExampleType?
```ts
type ExampleType = Promise<string>
type Result = MyAwaited<ExampleType> // string
```
> This question is ported from the [original article](https://dev.to/macsikora/advanced-typescript-exercises-question-1-45k4) by [@maciejsikora](https://github.com/maciejsikora)
> View on GitHub: https://tsch.js.org/189
*/
/* _____________ Your Code Here _____________ */
type MyAwaited<T extends Promise<unknown>> = T extends Promise<infer U>
? U extends Promise<any>
? MyAwaited<U>
: U
: T
/* _____________ Test Cases _____________ */
import type { Equal, Expect } from '@type-challenges/utils'
type X = Promise<string>
type Y = Promise<{ field: number }>
type Z = Promise<Promise<string | number>>
type Z1 = Promise<Promise<Promise<string | boolean>>>
type cases = [
Expect<Equal<MyAwaited<X>, string>>,
Expect<Equal<MyAwaited<Y>, { field: number }>>,
Expect<Equal<MyAwaited<Z>, string | number>>,
Expect<Equal<MyAwaited<Z1>, string | boolean>>
]
// @ts-expect-error
type error = MyAwaited<number>
/* _____________ Further Steps _____________ */
/*
> Share your solutions: https://tsch.js.org/189/answer
> View solutions: https://tsch.js.org/189/solutions
> More Challenges: https://tsch.js.org
*/