Answers for "typescript get the promise return type"

2

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);
});
Posted by: Guest on May-09-2020
1

typescript get the promise return type

type AsyncReturnType<T extends (...args: any) => any> =
	T extends (...args: any) => Promise<infer U> ? U :
	T extends (...args: any) => infer U ? U :
	any
Posted by: Guest on June-14-2021
0

typescript get type from promise

function promiseOne() {
  return Promise.resolve(1)
}
    
const promisedOne = promiseOne()
    
// note PromiseLike instead of Promise, this lets it work on any thenable
type ThenArg<T> = T extends PromiseLike<infer U> ? U : T
    
type PromiseOneThenArg = ThenArg<typeof promisedOne> // => number
// or
type PromiseOneThenArg2 = ThenArg<ReturnType<typeof promiseOne>> // => number
Posted by: Guest on May-27-2021

Code answers related to "typescript get the promise return type"

Code answers related to "TypeScript"

Browse Popular Code Answers by Language