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

Spreading a generic value allows adding unexpected properties to it #8745

Open
noppa opened this issue Sep 14, 2021 · 1 comment
Open

Spreading a generic value allows adding unexpected properties to it #8745

noppa opened this issue Sep 14, 2021 · 1 comment
Labels
bug Typing: soundness No false negatives (type checker claims that there is no error in the incorrect program) Typing: spread

Comments

@noppa
Copy link
Contributor

noppa commented Sep 14, 2021

Flow version: 0.159.0

Expected behavior

type A = {|foo: string, bar: string|}

function addBar<T>(val: T): T {
    return {...val, bar: 1}
}

const a: A = {foo: '', bar: ''}
const a2: A = addBar(a)

a2.bar.toUpperCase() // boom

I'd expect Flow to error at return {...val, bar: 1}, since nothing in <T> says that the object can have property bar.
Flow correctly errors if I try to directly assign to the object with val.bar = 1

Actual behavior

Flow doesn't show any errors, the code only fails at runtime. Flow allows both overwriting existing properties with incorrect types and adding completely new arbitrary properties to it (even though the type is exact).

  • Link to Try-Flow or Github repo:
    Try-Flow
@mvitousek mvitousek added Typing: soundness No false negatives (type checker claims that there is no error in the incorrect program) Typing: spread and removed needs triage labels Sep 20, 2021
@mvitousek
Copy link
Contributor

Thanks for the report! We've had some improvements to the soundness of the interaction of generics and spreads to the to-do list for a while, it looks like this hits on one of the ways that they interact in an unsound way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Typing: soundness No false negatives (type checker claims that there is no error in the incorrect program) Typing: spread
Projects
None yet
Development

No branches or pull requests

3 participants
@mvitousek @noppa and others