-
Notifications
You must be signed in to change notification settings - Fork 72
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
The bottleneck type definitions? #26
Comments
Hi. Yeah, a few people have attempted to write TypeScript type definitions before: Tattoo, Halo-client, but none are complete or perfect. The main issue I can see is that everybody uses (abuses) Just check that Tattoo link, you can see that they properly encoded the But there's a problem with those Tattoo type definitions, you can also see that they use If you want to make your type definitions generic and add all the missing methods, I'll be super happy to merge it and maintain it. Otherwise, I'll probably do it myself at some point in December. Pinging @garlicnation and @usergenic in case they have comments about this. |
@SGrondin I think we did that because the arguments to the scheduled function aka PromiseProducer were deemed unknown. Is it invoked with arguments? I don't see how they should be related to |
O_o Oh I understand your point now-- You're asking why we don't relate a specific type for |
Yes @usergenic you understand my point. If this were an ML language (like Haskell, SML or OCaml), I could do this: val schedule: ('a -> 'b -> 't promise) -> 'a -> 'b -> 't promise In other words, given:
Then Instead of using an interface for the PromiseProducer, is there a way to tell TypeScript that an argument is a function (and then defining the argument of that function)? That would solve the problem. |
Typescript doesn't seem to like the idea of PromiseProducer( ...args: A) Tried this: schedule<T,A>(promise: (...args: A) => Promise<T>, ...args: A):
Promise<T>; Got this: schedule<T,A>(promise: (...args: A) => Promise<T>, ...args: A):
~~~~~~~~~~
custom_typings/bottleneck.d.ts(4,29): error TS2370: A rest parameter must be of an array type. And it acts weird if you try to do this:
When arg types differ:
It can't seem to infer that A can be a type representing multiple types. So yeah not clear if there's a way to get current typescript to pattern match type sequence in args like that. |
Thanks for trying it out. I'll give it a shot over the holidays. |
I too want definitions for this. Typescript's type system doesn't have any way to express these functions in the general case for any number of arguments, however you can write overloaded function definitions for variants with different numbers of arguments like this: schedule<R>(fn: () => PromiseLike<R>): Promise<R>;
schedule<R, A1>(fn: (arg1: A1) => PromiseLike<R>, arg1: A1): Promise<R>;
schedule<R, A1, A2>(fn: (arg1: A1, arg2: A2) => PromiseLike<R>, arg1: A1, arg2: A2): Promise<R>; This can be repeated up to some reasonable limit of argument count. |
@alexperovich that's great! If you're interested in doing it, I'll very be happy to review and merge your PR. |
@SGrondin, hey I was wondering if you were planning on publishing this change to NPM anytime soon? Would love to have types for this library without having to pull down the module from Github. |
@jimboslicethat I'll be releasing it tonight or tomorrow night! |
@jimboslicethat @KaidenR Version |
Can anyone specify how these types are supposed to be installed? I am trying typescript in a project and installing @types/bottleneck as I did with other packages does not work, getting 404. Thanks! |
The types are part of the Bottleneck module itself, all you need to do is |
https://www.npmjs.com/package/@types/bottleneck
https://www.typescriptlang.org/docs/handbook/declaration-files/introduction.html
First attempt:
The text was updated successfully, but these errors were encountered: