-
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
Mixed refined to an object produces a read-only object, which breaks a lot of existing code. #7685
Comments
It is unsafe even if you refine type, because original type may have been string literal const m: {a: 'foo'} = {a: 'foo'}
function unsafeWrite(obj: mixed) {
if (typeof obj === 'object' && obj !== null) {
if (typeof obj.a === 'string') {
obj.a = 'bar' // unsafe
}
}
}
unsafeWrite(m)
m.a // bar |
Yes, this is an intentional change. Consider the example in the linked commit:
It is rarely necessary to use
Alternatively you could use generics or bounded generics if your function needs to be able to accept a variety of inputs. |
We're adding flow to a large code base, and Sure, I can simply replace mixed with an explicit type based on already written refinements, but that's something that the type system should be doing for me automatically, which is exactly what Why not have a warning instead when attempting to write properties of objects of mixed types? |
See these Flow issues for explanation: - facebook/flow#7684 - facebook/flow#7685 This is a breaking change. Fixes #3.
Flow version: 0.98
https://flow.org/try/#0GYVwdgxgLglg9mABMOcAUAjAhgJwFyIC2MAHgKYAmAlIgN4BQiiMwiaUAngA5lyvY5EAQgC8IxAHI4GAFZloExAB8lwgTQZMmOMlBA4kYEABtjAbkaIAvvUss2AgHRQAFjDABzRGPESAznCEum6eEhqWTE6u7l6+OFiuZDgSFkw2VkA
Expected behavior
Actual behavior
Also #7684
The text was updated successfully, but these errors were encountered: