Skip to content

Awaited 实现

假如我们有一个 Promise 对象,这个 Promise 对象会返回一个类型。在 TS 中,我们用 Promise 中的 T 来描述这个 Promise 返回的类型。请你实现一个类型,可以获取这个类型。

例如:Promise<ExampleType>,请你返回ExampleType的类型

ts
type ExampleType = Promise<string>

type Result = MyAwaited<ExampleType> // string
  • 在下方作答:
查看解答
ts
type MyAwaited<T> = T extends Promise<infer A> ? A extends Promise<any> ? MyAwaited<A>: A : never

Released under the MIT License.