generated at
Awaited型
0個以上のPromise型の入れ子をunwrapする型
v4.5からTypeScriptのbuilt-in typesとして提供されている
Promise.allの型付などに便利


定義
ts
type Awaited<T> = T extends null | undefined ? T : T extends object & { then(onfulfilled: infer F): any } ? F extends ((value: infer V, ...args: any) => any) ? // if the argument to `then` is callable, extracts the first argument Awaited<V> : // recursively unwrap the value never : // the argument to `then` was not callable T;
then に与えられる関数の第1引数 V を見て、ゴニョゴニョする


ts
type A = Awaited<Promise<string>>; // string type B = Awaited<Promise<Promise<number>>>; // number type C = Awaited<boolean | Promise<number>>; // boolean | number type D = Awaited<string>; // string