Search
 
SCRIPT & CODE EXAMPLE
 

TYPESCRIPT

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);
});
Comment

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
Comment

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
Comment

Get Promise type TypeScript

//return from promise
type promiseReturn<T extends Promise<unknown>> = T extends Promise<infer type>
	? type extends Promise<unknown>
		? promiseReturn<type>
		: type
	: never;
type prom = promiseReturn<Promise<string>>; //string
type prom_2 = promiseReturn<Promise<Promise<string>>>; //string
Comment

PREVIOUS NEXT
Code Example
Typescript :: typescript class validator validate enum array 
Typescript :: google sheets format number as duration formula 
Typescript :: the events calendar update the word event 
Typescript :: typescript open site in frame 
Typescript :: serenity.is center open dialog 
Typescript :: update a xml document if its not empty on c# 
Typescript :: class-validator not working nest-typescript-starter 
Typescript :: can ts object be strongly typed? 
Typescript :: squash commits on branch 
Typescript :: react tailwind css components npm 
Typescript :: tsconfig.json, Typescript 
Typescript :: how to keep only certian objects python 
Typescript :: npm install ionic2-calendar 
Typescript :: go Array’s length is part of its type. 
Typescript :: set constraints for UIView swift 
Typescript :: how to compile automatically in typescript 
Typescript :: regex exec returns null 
Typescript :: why important testng xml file 
Typescript :: flutter constructor default value 
Typescript :: c# to typescript 
Typescript :: excel check if value exists in range 
Typescript :: nullish coalescing typescript 
Typescript :: datasets in python github 
Typescript :: nest js joi usage 
Typescript :: typescript Identical Types 
Typescript :: typescript d ts meaning 
Typescript :: multiple hosts in same role 
Typescript :: .env.local is not working inside useEffect 
Typescript :: angular TS2377 
Typescript :: function which calculates the number of tweets that were posted per day. 
ADD CONTENT
Topic
Content
Source link
Name
9+1 =