Skip to content
Onsite

MyAwaited<T>

EasyP2Coding

Implement MyAwaited<T>, which unwraps the value a promise-like type resolves to, recursively. Promise<Promise<string>> becomes string, and an object with a compatible then method is treated as a promise.

T must be promise-like; MyAwaited<number> is a type error. Do not use the built-in Awaited.

Tests your type must pass

type X = Promise<string>
type Y = Promise<{ field: number }>
type Z = Promise<Promise<string | number>>
type Z1 = Promise<Promise<Promise<string | boolean>>>
type T = { then: (onfulfilled: (arg: number) => any) => any }

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>>,
  Expect<Equal<MyAwaited<T>, number>>,
]

// @ts-expect-error a number is not promise-like
type error = MyAwaited<number>

Hints

TypeScript
Run and Submit type-check your solution against the tests.