-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Make notSetValue optional for typed Records #1461
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
Make notSetValue optional for typed Records #1461
Conversation
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. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need the corporate CLA signed. If you have received this in error or have any questions, please contact us at [email protected]. Thanks! |
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Facebook open source project. Thanks! |
There are two other PRs (#1469, #1445) that address the same issue (#1463). This is the one that Does The Right Thing. I suspect that the bug originated in a one-to-one translation between Flow and TypeScript - the former treats In addition to fixing that mistranslation, this also fixes an issue that is shared between the Flow and TS annotations - they didn't properly describe the behavior when a key is passed that's not in the Record. This is legal, and results in a different return type. @morsecodist, could you add a commit to this PR applying the same fix to immutable.js.flow? (And then remove the part of the comment right above the TS version that describes the bug 😛.) |
Thank you for fixing this! |
@@ -2431,7 +2431,8 @@ declare module Immutable { | |||
* notSetValue will be returned if provided. Note that this scenario would | |||
* produce an error when using Flow or TypeScript. | |||
*/ | |||
get<K extends keyof TProps>(key: K, notSetValue: any): TProps[K]; | |||
get<K extends keyof TProps>(key: K, notSetValue?: any): TProps[K]; | |||
get<T>(key: string, notSetValue: T): T; |
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.
What if the key is set and the type of TProps[key]
is different from the T
value? Couldn't this result in a program that type checks but is incorrect?
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.
Do Records allow setting keys that are not in TProps
?
Tiny change to the type definitions.
First of all type-safe immutable records are glorious, thank you! I am a huge fan of what you are doing here. Secondly, your type declarations throw compiler errors for using the API as per the documentation. In your documentation for Record. You use
get
like so:Doing this with the current type definition produces the following error:
Expected 2 arguments, but got 1
My new type definition will give you the following behavior:
This seems to capture the API pretty well.
Dicussion
With type safe records you could forbid the
notSetValue
entirely. Since we can forcekey
to be a key ofTProps
we should never need anotSetValue
. Using one in these cases will probably be unintentional. But this means there are ways to validly use the API in JS that you can't in TypeScript, which is understandable not to want.