-
-
Notifications
You must be signed in to change notification settings - Fork 67
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
Add TypeScript definition #85
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
}>(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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
include
andexclude
for typing, as we can't test RegExp while typingIssueHunt Summary
Referenced issues
This pull request has been submitted to: