TypeScript friendly Utility functions for Developers
npm install utiljs-proTo install:
``bashnpm
npm install utiljs-pro
`ts
// isPromise
const promise = new Promise(res => setTimeout(() => res(1), 50)) as Promise | numberif (isPromise(obj)) { // true
obj; // hovering on obj shows the type as Promise
const d = await obj; // now hovering on d shows the type as number
} else {
obj; // hovering on obj shows the type as number
}
`
`ts
// isAsyncFunction
const asyncFn = (async () => { }) as unknown as (() => Promise) | (() => number)
if (isAsyncFunction Promise>(asyncFn)) { // true
const p = asyncFn(); // hovering on function shows the type as (() => Promise) and on p as Promise
const d = await p; // now hovering on d shows the type as Promise
} else {
const d = asyncFn(); // hovering on function shows the type as (() => number) and on d as number
}
``