typescript type of a return a promise code example
Example 1: typescript promise
new Promise<boolean>((res, rej) => {
res(true);
})
.then(res => {
console.log(res);
return false;
})
.then(res => {
console.log(res);
return true;
})
.then(res => {
console.log(res);
})
.catch(error => {
console.log('ERROR:', error.message);
});
Example 2: typescript get type from promise
function promiseOne() {
return Promise.resolve(1)
}
const promisedOne = promiseOne()
type ThenArg<T> = T extends PromiseLike<infer U> ? U : T
type PromiseOneThenArg = ThenArg<typeof promisedOne>
type PromiseOneThenArg2 = ThenArg<ReturnType<typeof promiseOne>>