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

Type guards / excess object literals property checking fails with Object.assign() #15963

Closed
18steps opened this issue May 20, 2017 · 2 comments
Closed
Labels
Duplicate An existing issue was already created

Comments

@18steps
Copy link

18steps commented May 20, 2017

Object spread is fixed in the nightly build, as per #13878 last commented on 2 days ago, but Object.assign() is not fixed, and it doesn't seem to be mentioned in any existing issues.

TypeScript Version: 2.3.2, 2.4.0-dev.20170519

Code

interface Limited {
  property: string;
}

const assignment = (): Limited => {
  return {
    property: 'this is fine'
  };
};

const assignment2 = (): Limited => {
  return {
    property: 'this is fine',
    property2: 'this is not fine, and shouldn\'t be',
  };
};

const nothing = {};
const somethingElse = {
  something: 'something'
};
const existing = {
  property: 'existing'
};

const assignment3 = (): Limited => {
  return {
    ...nothing,
    property2: 'this is not fine, and shouldn\'t be',
  };
};

const assignment4 = (): Limited => {
  return {
    ...somethingElse,
    property2: 'this is not fine, and shouldn\'t be',
  };
};

// - fails in 2.3.2 but not 2.4.0-dev.20170519
const assignment5 = (): Limited => {
  return {
    ...existing,
    property2: 'this should not be fine, and is not in 2.4.0-dev.20170519',
  };
};

// !
const assignment6 = (): Limited => {
  return Object.assign(existing, {
    property2: 'this should not be fine either, but is',
  });
};

Expected behavior:
Type guards should produce errors when attempting to assign a property not defined in the interface.

assignment5() and assignment6() should produce errors.

Actual behavior:
With Object.assign() or the Object spread operator, type guards fail to produce an error when attempting to assign a property not defined in the interface, as long as the required properties exist.

assignment5() and assignment6() does not produce an error.

@mhegazy
Copy link
Contributor

mhegazy commented May 22, 2017

this is tracked by #11100

Object spread is fixed in the nightly build, as per #13878 last commented on 2 days ago, but Object.assign() is not fixed, and it doesn't seem to be mentioned in any existing issues.

#13878 is a different issue than what you are reporting.

@mhegazy mhegazy added the Duplicate An existing issue was already created label May 22, 2017
@mhegazy
Copy link
Contributor

mhegazy commented Jun 6, 2017

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

@mhegazy mhegazy closed this as completed Jun 6, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 14, 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

2 participants