Skip to content

concat/typescript: add more precise type signature overloads for the common case #1151

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

Closed

Conversation

emmanueltouzery
Copy link
Contributor

(note: I've asked a stackoverflow question on the issue)

My issue with the current type signature for concat is that while it catches all the scenarios supported by the JS API, it's completely type-unsafe, including in its return type. The return type is the one that bit me, since it returns Collection<any,any>, if you don't cast after that, anything you'll do in a chaining call will compile without warnings, even if you use noImplicitAny.

The typescript compiler actually supports overloads and from my tests it actually picks up the more precise overload in my project and therefore gives me the precise return type of Collection<K,V>, giving me more type-safety.

I had also prepared another patch, with an extra overload:

concat(...values: { [key: string]: V }[]): Collection<any, V>;

with that extra overload (not really sure whether its definition is actually correct), and if you remove the original catch-all version, all the immutables tests actually pass, only one thing doesn't build:

let v2 = v1.concat(4, List.of(5, 6), [7, 8], Seq({a: 9, b: 10}), Set.of(11, 12), null);

and clearly no way to make that use-case type-safe. For the typescript bindings, it might actually make sense to enforce type-safety, and reject completely such scenarios. But if that's not an option, this PR already improves the situation at least on the return type, if not on type check of the parameters.

@facebook-github-bot
Copy link

Thank you for your pull request and welcome to our community. We require contributors to sign our Contributor License Agreement, and we don't seem to have you on file. In order for us to review and merge your code, please sign up at https://code.facebook.com/cla - and if you have received this in error or have any questions, please drop us a line at cla@fb.com. Thanks!

If you are contributing on behalf of someone else (eg your employer): the individual CLA is not sufficient - use https://developers.facebook.com/opensource/cla?type=company instead. Contact cla@fb.com if you have any questions.

@facebook-github-bot
Copy link

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Facebook open source project. Thanks!

@leebyron
Copy link
Collaborator

Great suggestion. In fact I think we can get even more specific than this, similar to how the map() method gets specific definitions for each type.

leebyron added a commit that referenced this pull request Mar 12, 2017
This adds significant improvements to the definitions for concat.

Fixes #1151
leebyron added a commit that referenced this pull request Mar 12, 2017
This adds significant improvements to the definitions for concat.

Fixes #1151
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants