logoProsperBao

Awaited

2022-03-21 01

题目来源(type-challenges)

问题

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

比如:Promise<ExampleType>,请你返回 ExampleType 类型。

这个挑战来自于 @maciejsikora 的文章:original article

type X = Promise<string>
type Y = Promise<{ field: number }>

type ResultX = Awaited<X>; // string
type ResultY = Awaited<Y>; // { field: number }

type Awaited<A> = /** 答案 **/

解答

type Awaited<T> = T extends Promise<infer U> 
    ? U extends Promise<infer F>
        ? Awaited<F> 
        : U
    : T

拆分

  1. Promise<T> 可能是多层嵌套的,所以需要递归处理拿到最里层的值
  2. 结合 inter U 占位嵌套,用三元表达式推断内层是否存在嵌套