Fix flow typings of partitionObjectByKey#214
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 - 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. |
|
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Facebook open source project. Thanks! |
|
It looks like this error isn't present in Node 4 (see Travis). It would be great if someone could review this PR. |
|
/cc @knowbody - do you know if there's a way to raise this up for review? |
|
cc @zpao |
|
I tweeted @zpao he said that he will look into it https://twitter.com/matzatorski/status/824346183183323149 |
| source: {[key: string]: Tv}, | ||
| whitelist: Set<string> | ||
| ): Array<Object> { | ||
| ): [{[key: string]: Tv}, {[key: string]: Tv}] { |
There was a problem hiding this comment.
Internally we just have a $FlowFixMe here because upgrade codemods, but I'm curious what the right thing is. I tried to apply this change internally but some other code starts failing because we have some uses where Tv isn't consistent (primary use seems to be splitting a props object in a React component). So we can't take it as is. I don't know how to type that correctly though - mixed (instead of the type parameter) doesn't seem to work (have to add more type checks in places) and any is never awesome. Just changing the return signature to [Object, Object] to make sure the tuple is handled correctly (which seems to be the thing to fix in 0.38) and leaving the rest as it was seems to be fine. But it also seems overly generic.
cc @gabelevi to perhaps comment?
There was a problem hiding this comment.
It seems like the caller of this function might need to do a type cast. My guess is that Flow has to define Tv as the most generic supertype of all the object values, which is often something like mixed for React component props.
So the caller either should type cast the argument as Object or {[key: string]: any}, or type cast the returned partitions as {[key: string]: whateverMakesSense}.
Flow might be able to do something smart in this specific instance with $Keys, not sure if the complexity is worth it compared to using Object though.
There was a problem hiding this comment.
I don't have a nice solution either, but it would work if we change partitionObject to the following and keep partitionObjectByKey unchanged:
function partitionObject(
object: Object,
callback: (value: string, key: string, object: Object) => boolean,
context?: any
): Array<Object> {
...
}There was a problem hiding this comment.
Try [Object, Object]
Seems like a good compromise between the current troublesome annotation and the actual tuple type.
There was a problem hiding this comment.
@zpao - can confirm that [Object, Object] worked internally, if you want to sync D4479141
|
@lgeiger - want to make it |
to match partitionObject.
|
@zpao Sorry for the delay. I updated the PR 👍 |
* ⬆️ flow-bin@0.38.0 * [fbjs] Fix flow typings of partitionObjectByKey to match partitionObject. (cherry picked from commit 8d44778)
|
Thanks! I just published v0.8.9 with this 🎉 |
flow-bin@0.38.0will throw the following error:This PR fixes the flow typings of
partitionObjectByKeyto matchpartitionObject.We discovered this problem in https://github.com/nteract/nteract/pull/1349 while trying to upgrade
flow-bin.