Hello World
문제
type HelloWorld = any
type test = Expect<Equal<HelloWorld, string>>
풀이
type HelloWorld = string;
Awaited
문제
type MyAwaited<T> = any
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 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>>,
]
type error = MyAwaited<number>
풀이
type thenable<T> = { then: (onfulfilled: (arg: T => any) => any }
type MyAwait<T extends Pormise<any> | tenable<any>> =
T extends Promise<infer Inner> ?
Innder extends Promise<any> ?
MyAwait(Inner) :
Inner :
Textends thenable<innder U> ?
U :
never;