pify icon indicating copy to clipboard operation
pify copied to clipboard

Add TypeScript definitions

Open tom-sherman opened this issue 3 years ago • 4 comments

Closes #74

Pros and cons compared to #85

Pros:

  • No need for as many overloads
  • Supports as many generics as you like
  • Support for as many callback arguments as you like when multiArgs: true. Although not sure if this is in scope in the context of the errorFirst options.

Cons:

  • pify((v: number) => {})() returns never instead of erroring. A bit of annoying DX but isn't unsafe.

Todo:

  • [ ] Promisifying modules with includes and excludes
  • [ ] Document clearly that overloaded functions are not supported and only the final overload will be chosen: https://github.com/microsoft/TypeScript/issues/32164
  • [ ] Support excludeMain
  • [ ] Support errorFirst

tom-sherman avatar Dec 18 '21 15:12 tom-sherman

Still interested in finishing this? :)

sindresorhus avatar Jan 23 '22 10:01 sindresorhus

@sindresorhus Sure! I'll pick this up when I have some time 😄

Just a note, I'm not sure my top todo of "Promisifying modules with includes and excludes" is possible in all cases. Do you have any ideas around that?

My main concern is that you can't get it fully type safe, so any behaviour here will eventually lead to runtime errors. It may be better to just ignore them on a type level.

tom-sherman avatar Jan 23 '22 11:01 tom-sherman

Just a note, I'm not sure my top todo of "Promisifying modules with includes and excludes" is possible in all cases. Do you have any ideas around that?

In what cases is it not possible?

sindresorhus avatar Jan 23 '22 11:01 sindresorhus

My main concern is that you can't get it fully type safe, so any behaviour here will eventually lead to runtime errors. It may be better to just ignore them on a type level.

That's fine as long as the cases are documented.

sindresorhus avatar Jun 06 '22 12:06 sindresorhus

@tom-sherman Friendly bump :)

sindresorhus avatar Aug 30 '22 07:08 sindresorhus

Now I'm revisiting this it looks like excludes/includes are possible to make safe for the most part. I think even exuding the sync methods by default is possible with template literal types.

tom-sherman avatar Aug 31 '22 10:08 tom-sherman

@sindresorhus I'm not sure it's possible to support "module functions" here ie. modules with methods. TypeScript doesn't really allow you to work with a type that's callable and also has function properties. We would need to make excludeMain produce never as the return type. What do you think?

The workaround would be to pify each method and the main function separately.

Edit:

I've changed the semantics to be more clear in https://github.com/tom-sherman/pify/commit/4987750dc53d9913a0dc627ed37ec5e304ebdacb

Hopefully this is ok!

tom-sherman avatar Aug 31 '22 13:08 tom-sherman

Looks good. Thank you :)

sindresorhus avatar Sep 02 '22 11:09 sindresorhus

@tom-sherman Are you sure there's no way to handle overloads? Because I already hit this problem in multiple places. For example, https://github.com/sindresorhus/got/runs/8157202709?check_suite_focus=true where the overloads are:

export function createCSR(options: CSRCreationOptions, callback: Callback<{ csr: string, clientKey: string }>): void;
export function createCSR(callback: Callback<{ csr: string, clientKey: string }>): void;

This limitation makes it kinda annoying to use pify and the old types (@types/pify) could handle it.

sindresorhus avatar Sep 02 '22 14:09 sindresorhus

Can you share what the output of pify(createCSR) would be usinng @types/pify? As I understand it you'd just get Promise<any>? If so then @types/pify handles it by not handling it at all, there is very little type safety here.

The issue we have is that there is no easy way to collect all arguments of an overloaded function as a union of tuples, as far as I'm aware. See https://github.com/microsoft/TypeScript/issues/32164

I'll have another read through this issue though, maybe there's a workaround now.

tom-sherman avatar Sep 02 '22 14:09 tom-sherman

Does the workaround I added to the readme help for you? I get that it's not very ergonomic but should work in all cases.

https://github.com/sindresorhus/pify#why-is-pify-choosing-the-last-function-overload-when-using-it-with-typescript

tom-sherman avatar Sep 02 '22 14:09 tom-sherman

Does the workaround I added to the readme help for you? I get that it's not very ergonomic but should work in all cases.

Yes, that does work: https://github.com/sindresorhus/got/pull/2120/commits/2cd86007f179abd951cf80fb5bb2d4322b5505a3

sindresorhus avatar Sep 02 '22 14:09 sindresorhus