Skip to content

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

Open

Description

@noppa

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

Activity

  1. added
    Typing: soundness
    and removed on Sep 20, 2021
  2. mvitousek commented on Sep 20, 2021

    @mvitousek
    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Typing: soundnessTyping: spreadbug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions