1new Promise<boolean>((res, rej) => {
2 res(true);
3})
4.then(res => {
5 console.log(res);
6 return false;
7})
8 .then(res => {
9 console.log(res);
10 return true;
11})
12 .then(res => {
13 console.log(res);
14})
15 .catch(error => {
16 console.log('ERROR:', error.message);
17});
1type AsyncReturnType<T extends (...args: any) => any> =
2 T extends (...args: any) => Promise<infer U> ? U :
3 T extends (...args: any) => infer U ? U :
4 any
1function promiseOne() {
2 return Promise.resolve(1)
3}
4
5const promisedOne = promiseOne()
6
7// note PromiseLike instead of Promise, this lets it work on any thenable
8type ThenArg<T> = T extends PromiseLike<infer U> ? U : T
9
10type PromiseOneThenArg = ThenArg<typeof promisedOne> // => number
11// or
12type PromiseOneThenArg2 = ThenArg<ReturnType<typeof promiseOne>> // => number
13