Skip to content
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

Unannotated object's properties are not treated as invariant when passed as arguments #8218

Open
danny-does-stuff opened this issue Dec 6, 2019 · 2 comments
Labels
bug Typing: property variance Typing: soundness No false negatives (type checker claims that there is no error in the incorrect program)

Comments

@danny-does-stuff
Copy link

Flow version: 0.113.0

Expected behavior

Objects' properties should be treated as invariant when passed to a function.

Actual behavior

When passed to a function, a simple unannotated object is incorrectly allowed. It appears that if the object is annotated with a type, flow correctly catches the issue (see flow try for more info)

function func(arg: { key: { shared: number } }) {
  arg.key = { shared: 10 }
}

const object = { key: { shared: 10, bonus: 'string' } }
func(object) // This should not be allowed, but it is

Interestingly enough, if the property in question is nested another layer, the issue is correctly caught. This is seen in the Flow Try in the commented section.

@jamesisaac
Copy link
Contributor

I don't really see the problem with Flow's behaviour here. If you try and access unannotatedA.key.extra before the call of mutatesKey(unannotatedA), it works. If you try and access it after the call. Flow correctly tells you that the property extra is missing. So Flow seems to be correctly following the mutation of types your function creates, which is fine because it's not restricted by any explicit type annotation.

As for why it doesn't allow that with an extra level of nesting added, I'm not sure, but I would have thought the more lax behaviour in the former case is actually preferable?

@danny-does-stuff
Copy link
Author

@jamesisaac Oh interesting point. The way I understood it, if you make an unannotated object, flow infers the type when the variable is set so I would expect it to not allow “updating” the type later on (by reassigning properties). Regardless, it seems that it should behave the same way for any level of nesting

@mvitousek mvitousek added Typing: property variance Typing: soundness No false negatives (type checker claims that there is no error in the incorrect program) and removed needs triage labels Jan 28, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Typing: property variance Typing: soundness No false negatives (type checker claims that there is no error in the incorrect program)
Projects
None yet
Development

No branches or pull requests

3 participants