Skip to content

Type intersection on type with nested type breaks extra properties error #29678

Closed
@Emily

Description

@Emily

TypeScript Version: 3.4.0-dev.20190131 (I spot checked versions since 3.0.1, all had this issue

Search Terms: intersection extra properties

Code

type Problematic = { one?: string } & { nested?: { two?: string } }
function problem(input: Problematic) {}

// nested containing bad property unexpectedly doesn't cause error
problem({
  nested: {
    two: 'hello',
    bad: 123
  }
})

Expected behavior:
bad: 123 should cause an extra properties error.

Actual behavior:
bad: 123 does not cause the extra properties error.

Playground Link: expanded examples

Related Issues: #28642

For anyone tripped up by this, there's a convenient work-around:

type Merge<T extends object> = { [K in keyof T]: T[K] };
type Problematic = { one?: string } & { nested?: { two?: string } }
function problemGone(input: Merge<Problematic>) {}

// works as expected
problemGone({
  nested: {
    two: 'hello',
    bad: 123
  }
})

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