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