-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
Fix object spreading, issues 6108, 7242, 5243, 7210, 7253, 6357, 5253, 4682, 6800, 981, 7048 and more #7298
Conversation
match Property.read_t p, to_obj with | ||
| Some _, (DefT (_, ObjT ob)) when not ob.flags.frozen -> | ||
Context.set_prop cx ob.props_tmap x p | ||
| Some t, to_obj -> |
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.
this pattern match branch was still needed for these to work: https://github.com/facebook/flow/blob/master/tests/destructuring/destructuring.js#L61
https://github.com/facebook/flow/blob/master/tests/object_assign/non_objects.js
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.
https://github.com/facebook/flow/blob/master/tests/destructuring/destructuring.js#L61 does not work without | Some t, to_obj ->
branch because this generates OpenT instead of ObjT. Otherwise | Some t, to_obj ->
branch could be unnecessary altogether.
ec5199f
to
99e058a
Compare
|
||
type NestedOb = {|+foo: {| +bar: {| +foo: number |} |}|}; | ||
declare var update2: NestedOb; | ||
({ foo: { bar: { foo: 123 } }, ...update2 }: NestedOb); // Ok |
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.
not sure if firendly_errors folder is correct folder for these, but https://github.com/facebook/flow/blob/master/tests/friendly_errors/prop-variance.js is perhaps the closest to above tests that I found.
I run this for our codebase and due to this Flow detected dozens of new inconsistent types. In most cases inconsistent maybe types, so plenty of potential runtime failures that has previously been undetected. So this not only makes Flow more pleasant to use, but also more sound. |
@FezVrasta That's unfortunately different issue. |
Gotcha! Thanks |
Looking forward to fix hundreds of new errors once this gets merged! 😌 |
@facebook Could the community get some update on this critical bug? We were promised a really promising, communicative 2019 and got told this issue will be code reviewed soon, but then it's another month of total silence. It actually makes me feel pretty bad that we have to do something a bit pushy like this to get some progress update, kinda creates a tension between the community and the maintainers. Instead of trying to recruit more devs, I'd say it might actually be more valuable in finding a competent project manager/owner that can help kicking start a healthy communication loop. |
This PR is in a bit limbo state at the moment I believe. What I've understood, Flow team is working on totally refactored object model which should also fix spreading issues as well as many other problems with current way of handling objects in Flow. For this reason I don't want to put more effort in this at the moment. Maybe @jbrown215 can shed some light on where the refactoring is going? |
@villesau thanks for the update! May I ask how did you get these info? I would have expected the Flow team to communicate here, after the Medium post they published about the 2019 promises. Thanks! |
We've been in touch with @villesau on discord. We actually interact with him almost daily there to talk about a variety of issues, and in general we've found the discord to be a source of very good discussions. If you want the more frequent and less formal updates from the Flow team, I recommend joining the discord: https://discord.gg/8ezwRUK
Yes. We are in the middle of a large refactor of our object model. @samwgoldman or I will publish a larger blog post that covers the necessity of the changes we're making. The reason we haven't moved forward on this PR is because of a significant performance regression and because we are still unsure about the correctness. Fixing object spreads is also my current project (and the vast majority of my day-to-day work). There are a lot more issues than just this one, and I'm aiming to fix all of them. I've been making good progress but I don't want to give an estimate for when it will be released because there are still a few roadblocks that I'm not sure how to get past. |
@jbrown215 I'm sure there is more to it but I'd still like to point out that releasing some fixes usually is better than releasing no fixes ;) I'm saying this without knowing anything about the implications but sometimes I also need to be reminded of the simple fact that working in small iterations generates more value for end-user than releasing things in large batches. |
I agree! We just aren't positive this is a fix that doesn't introduce other issues, but we also know it introduces a performance issue. In particular, this fix relies on the constraints flow generates being evaluated in a certain order. There are some cases where that invariant may hold, but it's something we usually assume is false unless a convincing argument is given. That could lead to something like |
Thanks to facebook/flow#6906 for this `Partial` type -- a workaround for `$Shape` not working correctly. See also facebook/flow#7298.
Thanks to facebook/flow#6906 for this `Partial` type -- a workaround for `$Shape` not working correctly. See also facebook/flow#7298.
So is the buggy object-spreading the reason why this isn't working? Or am I doing something else wrong? type A = { a: string };
type B = A & { b: string };
type C = A & { b: number };
const testB: B = { a: 'test', b: 'test b' };
function test(obj: B): C {
return {
...obj,
b: 23,
};
}
test(testB);
|
Pretty sure this is it. It also doesn't work with type spreads (which are generally more "predictable" than |
@FezVrasta 👋 heya, did you happen to find the right issue for or Seems like the type if conflicting with the inferred type of the object? I'm also just trying to provide default options for an argument |
Any updates here? |
Relate issue const obj = { ...{ hi: () => 1 }, ...{ hi: (x: number) => 'hi' } };
obj.hi(5); |
Thanks to facebook/flow#6906 for this `Partial` type -- a workaround for `$Shape` not working correctly. See also facebook/flow#7298.
This week, @jbrown215 landed some major fixes to spread which will come out in Flow v0.111.0, scheduled for release next week. I haven't checked every associated issue to make sure that they were all fixed, but these changes address most (or all?) of the known issues with spread. Jordan is working on a blog post with more details. I think that Jordan's changes accomplish the goals of this PR, so I'm going to close it. @villesau, please let us know if that's not the case. |
Update: I went through the list of associated issues and @jbrown215's recent changes address all but one of them. |
facebook/flow#7298 was resolved in Flow v0.111.0
This fixes bunch of issues with object spreading. Or actually one root cause that had many different symptoms. Now the type is replaced when spread, when before flow made union type out of it, which is not how it should be. In most cases Context.set_prop is just enough: You just want to set the type, and not to do any checks or unions there. Type checks are handled elsewhere so this does not weaken the type safety but makes spreading more sound instead.
To demonstrate the impact, this fixes at least following issues and likely even more that I didn't find or are not obviously related to spreading:
bar
is read-only in object type [1] but writable in object literal [2]. #7242The tests follows this naming convention: https://github.com/facebook/flow/blob/master/tests/spread/issue_4152.js
The react_jsx tests actually had wrong expectations, which I changed as well.
For anyone interested in fixing similar issues, here are instructions on how to debug Flow: #4181 (comment)