gulpjs / async-done

Allows libraries to handle various caller provided asynchronous functions uniformly. Maps promises, observables, child processes and streams, and callbacks to callback style.
MIT License
70 stars 21 forks source link

Add type definitions #47

Closed demurgos closed 6 years ago

demurgos commented 6 years ago

This also updates the devDependency rx@4 to rxjs@5 because it bundles its own types. This is a semver minor update (feature).

Two comments regarding these types:

Closes gulpjs/async-done#41

/cc @BurtHarris @phated

demurgos commented 6 years ago

Thanks for checking. I usually prefer to be more conservative when I have a doubt (e.g. Stream or ReadableStream) so having someone more familiar with the lib and the values actually expected is good.

Before Typescript 2 and the existence of @types on npm, I wrote definitions for a tool called typings. This is now in maintenance but maybe some of the members are available:

@unional @blakeembrey Do you have some time to review this PR? It is pretty short and adds types for async-done. I have few questions where you may help me:

unional commented 6 years ago

Is there a way to unify my VoidCallback and Callback types without Microsoft/TypeScript#5453?

It is also related to this: https://github.com/Microsoft/TypeScript/issues/12400 It is marked as committed for 2.8

What is your current strategy to handle dependencies modifying the environment?

Not sure what you mean, global augmentation?

Is there a trick to prevent (): void => {} from matching (done: VoidCallback): void => {}.

I assume its used in a callback (can't find that in your PR from skimming)? AFAIK, there is no way to prevent that.

🌷

demurgos commented 6 years ago

@unional

Is there a way to unify my VoidCallback and Callback types without Microsoft/TypeScript#5453?

It is also related to this: Microsoft/TypeScript#12400 It is marked as committed for 2.8

Great! I'm still waiting for variadic types but the issue you linked should help with this case. Glad to see it got scheduled.

What is your current strategy to handle dependencies modifying the environment?

Not sure what you mean, global augmentation?

Sorry I was still calling them "ambient declarations", but yes: this is what I had in mind. I am mentioning variable declarations that let you use variables without having to import them first. Example in @types/node and @types/mocha. Support for these declarations was really the strong point of typings and I am still not sure what's the best way to deal with them today.

I assume its used in a callback (can't find that in your PR from skimming)? AFAIK, there is no way to prevent that.

Yes, it was used in callbacks. The current tests only check that the file compile so I cannot test for expected compilation errors. This case is mentioned in a comment: here is the relevant part.

// I don't think TS is currently able to prevent the current code from compiling
// (`neverDone` matches with `(done: VoidCallback) => void` for example)
// asyncDone(neverDone, function(err: Error | null, result?: number): void {
//  console.log("Done");
// });

Passing neverDone fails at runtime, I don't think it's possible to catch this with type-checking.

I don't think it is supported currently, but I had an idea like this:

interface AsyncTask {
  (done: VoidCallback): void;
  length: 1;
}

This would add the constraint that functions have to have exactly one parameter. But I think that you are right: there are too many edge cases such as arguments or (...args) (or some crazier things like proxies) to make it works. That's why I said at the beginning that it's hard to get types right when callbacks are involved.

demurgos commented 6 years ago

Most of the comments were addressed: would it be possible to merge and publish it (semver feature)?

Regarding my questions in this thread: it's safer to keep global definitions (Node) as devDependencies and the issue to unify VoidCallback and Callback was postponed so it's better to not wait for it.

phated commented 6 years ago

@demurgos I've reviewed everything and it seems to make sense with my minimal knowledge (thanks for all the docs). I'll get this published.

phated commented 6 years ago

@demurgos whoops, the index.d.ts wasn't added to "files" in the package.json so I publishing as 1.3.1