Awaited型
0個以上のPromise型の入れ子をunwrapする型
Promise.allの型付などに便利
定義
code: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を見て、ゴニョゴニョする
例
code: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