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

Spread operator + unknown key doesn't result in a type error #14706

Closed
brandonwamboldt opened this issue Mar 17, 2017 · 4 comments
Closed

Spread operator + unknown key doesn't result in a type error #14706

brandonwamboldt opened this issue Mar 17, 2017 · 4 comments
Labels
Duplicate An existing issue was already created

Comments

@brandonwamboldt
Copy link

TypeScript Version:

TypeScript 2.2.1

Code

interface TestProps {
  foo: string;
  bar: string;
}

function test(mode: number, vars: TestProps): TestProps {
  switch (mode) {
    case 1:
      return { 
        foo: 'a',
        bar: 'b',
        other: 'c', // This is an error
      };
    case 2:
      return {
        ...vars,
        other: 'c', // This SHOULD be an error
      }
  }

  return {
    foo: 'a',
    bar: 'b',
  }
}

Expected behavior:

The spread operator with an unknown attribute should show a type error.

Actual behavior:

The spread operator with an unknown attribute does not show a type error.

@RyanCavanaugh RyanCavanaugh added the Bug A bug in TypeScript label Mar 17, 2017
@aaronbeall
Copy link

This is a pretty big issue I've run into while using Redux, where all state changes require copying the object keys and overwriting a subset, just like case 2. I've had to make a special function that wraps Object.assign but catches unknown props.

@mhegazy
Copy link
Contributor

mhegazy commented Mar 20, 2017

Duplicate of #13878, this is behaving as design, the excess property checks are disabled for spread. you can read more about this in #12997

@mhegazy mhegazy added Duplicate An existing issue was already created and removed Bug A bug in TypeScript labels Mar 20, 2017
@brandonwamboldt
Copy link
Author

I read through both tickets and don't really understand why they are disabled. Could you please clarify?

@aaronbeall
Copy link

aaronbeall commented Mar 21, 2017

@mhegazy @brandonwamboldt I just put a few thoughts on #12997 . I hope this decision gets reconsidered. Thanks!

@microsoft microsoft locked and limited conversation to collaborators Jun 21, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants