Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improv: update types #9

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
improv: update types
Use infer to retrieve function return type
  • Loading branch information
Eywek authored Dec 30, 2019
commit fb9b433c03009a66ce1c5f9a050ff570fa15a0ae
11 changes: 6 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
export type AWAIT_OF_RETURN_TYPE = [any?, Error?];
type Unpromisify<T> = T extends Promise<infer R> ? R : T
type ReturnType<T> = T extends (...args: any[]) => infer T ? Unpromisify<T> : any

export function of(promise: Promise<any>): Promise<AWAIT_OF_RETURN_TYPE> {
export function of<T> (promise: Promise<ReturnType<T>>): Promise<[undefined, Error] | [ReturnType<T>]> {
return Promise.resolve(promise)
.then((r: any): AWAIT_OF_RETURN_TYPE => [r])
.then((r: ReturnType<T>): [ReturnType<T>] => [r])
.catch(
(e: Error | undefined | null): AWAIT_OF_RETURN_TYPE => {
(e: Error | undefined | null): [undefined, Error] => {
if (e === undefined || e === null) {
return [e, new Error("Rejection with empty value")];
return [undefined, new Error("Rejection with empty value")];
}

return [undefined, e];
Expand Down