求star

开源不易,喜欢请点个star吧

Ocean Han
59 字
1 分钟
Ts类型体操

Challenge#

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?

type ExampleType = Promise<string[]>;

type Result = MyAwaited<ExampleType>; // string[]

Answer#

type MyAwaited<T extends Promise<unknown>> = T extends Promise<infer U>
  ? U extends Promise<unknown>
    ? MyAwaited<U>
    : U
  : never;
Ts类型体操
https://blog.oceanh.top/posts/frontend/ts类型体操-awaited/
作者
Ocean Han
发布于
2023-08-10
许可协议
CC BY-NC-SA 4.0
最后修改时间
2024-08-10 10:08:49