Conversation
|
Not sure whether it's possible to do this here, but did you consider using variadic tuple types? See: sindresorhus/p-all#9 |
My previous attempt to do something along the lines failed to handle generic functions and was typing everything generic as unknown, I found an issue from 2014 I think which kinda pointed me to it being impossible without overloads(though the catch all version in this branch inherits some of it, and works for everything except generics as far as I can tell). But I'll look into this implementation too in case I missed something. |
@sindresorhus Worked out after some weird massage. Also cleaned up a bit. |
|
Many of the return types should be |
| type R1 = 'R1'; | ||
| type R2 = 'R2'; | ||
| let someBoolean:boolean = Math.random() > 0.5; | ||
| const a1:A1 = 'A1'; |
There was a problem hiding this comment.
| const a1:A1 = 'A1'; | |
| const a1: A1 = 'A1'; |
Applies in many other places
| const a1:A1 = 'A1'; | ||
| const a2:A2 = 'A2'; | ||
|
|
||
| let function00 = (cb: (err: Error | undefined) => any) => { }; |
There was a problem hiding this comment.
| let function00 = (cb: (err: Error | undefined) => any) => { }; | |
| let function00 = (callback: (error: Error | undefined) => any) => {}; |
Applies in many other places
| })); | ||
|
|
||
| declare function generic10<A0>(a0:A0, cb:(error: Error | null | undefined) => any): any; | ||
| declare function generic01<R0>(cb:(error: Error | null | undefined, r0:R0) => any): any; |
There was a problem hiding this comment.
| declare function generic01<R0>(cb:(error: Error | null | undefined, r0:R0) => any): any; | |
| declare function generic01<R0>(callback: (error: Error | null | undefined, r0: R0) => any): any; |
| declare function realLifeFunction1(url: string, callback: (error?: Error | null | undefined, response?: { statusCode: number }, body?: string) => void): void; | ||
| expectType<(url: string) => Promise<[{ statusCode: number }?, string?]>>(pify(realLifeFunction1, { multiArgs: true })) | ||
| declare function realLifeFunction2(url: string, callback: (response?: { statusCode: number }, body?: string) => void): void; | ||
| expectType<(url: string) => Promise<[{ statusCode: number }?, string?]>>(pify(realLifeFunction2, { multiArgs: true, errorFirst: false })) |
There was a problem hiding this comment.
| expectType<(url: string) => Promise<[{ statusCode: number }?, string?]>>(pify(realLifeFunction2, { multiArgs: true, errorFirst: false })) | |
| expectType<(url: string) => Promise<[{statusCode: number}?, string?]>>(pify(realLifeFunction2, {multiArgs: true, errorFirst: false})) |
|
|
||
| expectAssignable<{ | ||
| readFile: (path: string) => Promise<Buffer | undefined>, | ||
| }>(pify(fs, {exclude:['exists']})); |
There was a problem hiding this comment.
| }>(pify(fs, {exclude:['exists']})); | |
| }>(pify(fs, {exclude: ['exists']})); |
| /** | ||
| Returns a `Promise` wrapped version of the supplied function or module. | ||
| @param input - Callback-style function or module whose methods you want to promisify. | ||
| @returns Wrapped version of the supplied function or module. |
There was a problem hiding this comment.
| @returns Wrapped version of the supplied function or module. | |
| @returns Wrapped version of the supplied function or module. | |
Applies in many other places
| } | ||
|
|
||
| /** | ||
| Returns a `Promise` wrapped version of the supplied function or module. |
There was a problem hiding this comment.
| Returns a `Promise` wrapped version of the supplied function or module. | |
| Returns a `Promise` wrapped version of the supplied function or module. | |
| T extends [...infer _, (infer R)?] ? R | undefined: | ||
| never; | ||
|
|
||
| type ExceptLast<T extends any[]> = T extends [ ...infer Head, any ] ? Head : any[]; |
There was a problem hiding this comment.
| type ExceptLast<T extends any[]> = T extends [ ...infer Head, any ] ? Head : any[]; | |
| type ExceptLast<T extends any[]> = T extends [...infer Head, any] ? Head : any[]; |
| type LastParameter<T extends AnyFunction> = Last<Parameters<T>>; | ||
| type ParametersExceptLast<T extends AnyFunction> = ExceptLast<Parameters<T>>; | ||
|
|
||
| type SelectByOptionOr<O extends boolean, TTrue, TFalse> = |
There was a problem hiding this comment.
The various utility types here need a short doc comment about what they do.
|
|
||
| interface PifyOptions { | ||
| /** | ||
| By default, the promisified function will only return the second argument from the callback, which works fine for most APIs. This option can be useful for modules like `request` that return multiple arguments. Turning this on will make it return an array of all arguments from the callback, excluding the error argument, instead of just the second argument. This also applies to rejections, where it returns an array of all the callback arguments, including the error. |
There was a problem hiding this comment.
| By default, the promisified function will only return the second argument from the callback, which works fine for most APIs. This option can be useful for modules like `request` that return multiple arguments. Turning this on will make it return an array of all arguments from the callback, excluding the error argument, instead of just the second argument. This also applies to rejections, where it returns an array of all the callback arguments, including the error. | |
| By default, the promisified function will only return the second argument from the callback, which works fine for most APIs. This option can be useful for modules like `request` that return multiple arguments. Turning this on will make it return an array of all arguments from the callback, excluding the error argument, instead of just the second argument. This also applies to rejections, where it returns an array of all the callback arguments, including the error. | |
|
Are you sure this is the simplest way it could be done? It might be. I'm just hoping there are ways to further simplify it. |
|
@stroncium Bump :) |
|
Bump |
@sindresorhus I think it can be simplified quite a bit, I've made a start over on #86 |
|
Closing in favor of #86 |
Solves #74
includeandexcludefor typing, as we can't test RegExp while typingIssueHunt Summary
Referenced issues
This pull request has been submitted to: