Skip to content

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

@18steps

Description

@18steps

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions