Skip to content

Excess property checking only applies to half of an intersection #30715

Closed
@brunostuani

Description

@brunostuani

TypeScript Version: 3.3.333

Search Terms:
Type infer array nested interface structure

Code

interface ValueOnly {
  value: number | null;
}

interface ValueAndKey {
  key: string | null;
  value: number | null;
}

interface ValueOnlyFields {
  fields: Array<ValueOnly>;
}

interface ValueAndKeyFields {
  fields: Array<ValueAndKey>;
}

interface BugRepro {
  dataType: ValueAndKeyFields & ValueOnlyFields;
}

interface BugReproWithoutArray {
  dataType: ValueAndKey & ValueOnly;
}

// This. Having the "key" errors out because of extra "key" field,
// while not having the "key" makes it complain that "key" is missing.
const repro: BugRepro = {
  dataType: {
    fields: [{
      key: 'bla',
      value: null,
    }],
  }
}

// Without nesting the type inside of an array, it can infer the type nicely.
const reproWithoutArray: BugReproWithoutArray = {
  dataType: {
    key: 'bla',
    value: null,
  },
}

Expected behavior:
Compiler should not complain that "key" is defined.

Actual behavior:
Compiler cannot identify that the array is neither Array, and Array without me casting it.

Playground Link:
https://www.typescriptlang.org/play/#src=interface%20ValueOnly%20%7B%0D%0A%20%20value%3A%20number%20%7C%20null%3B%0D%0A%7D%0D%0A%0D%0Ainterface%20ValueAndKey%20%7B%0D%0A%20%20key%3A%20string%20%7C%20null%3B%0D%0A%20%20value%3A%20number%20%7C%20null%3B%0D%0A%7D%0D%0A%0D%0Ainterface%20ValueOnlyFields%20%7B%0D%0A%20%20fields%3A%20Array%3CValueOnly%3E%3B%0D%0A%7D%0D%0A%0D%0Ainterface%20ValueAndKeyFields%20%7B%0D%0A%20%20fields%3A%20Array%3CValueAndKey%3E%3B%0D%0A%7D%0D%0A%0D%0Ainterface%20BugRepro%20%7B%0D%0A%20%20dataType%3A%20ValueAndKeyFields%20%26%20ValueOnlyFields%3B%0D%0A%7D%0D%0A%0D%0Ainterface%20BugReproWithoutArray%20%7B%0D%0A%20%20dataType%3A%20ValueAndKey%20%26%20ValueOnly%3B%0D%0A%7D%0D%0A%0D%0A%2F%2F%20This.%20Having%20the%20%22key%22%20errors%20out%20because%20of%20extra%20%22key%22%20field%2C%0D%0A%2F%2F%20while%20not%20having%20the%20%22key%22%20makes%20it%20complain%20that%20%22key%22%20is%20missing.%0D%0Aconst%20repro%3A%20BugRepro%20%3D%20%7B%0D%0A%20%20dataType%3A%20%7B%0D%0A%20%20%20%20fields%3A%20%5B%7B%0D%0A%20%20%20%20%20%20key%3A%20'bla'%2C%0D%0A%20%20%20%20%20%20value%3A%20null%2C%0D%0A%20%20%20%20%7D%5D%2C%0D%0A%20%20%7D%0D%0A%7D%0D%0A%0D%0Aconst%20reproWithoutArray%3A%20BugReproWithoutArray%20%3D%20%7B%0D%0A%20%20dataType%3A%20%7B%0D%0A%20%20%20%20key%3A%20'bla'%2C%0D%0A%20%20%20%20value%3A%20null%2C%0D%0A%20%20%7D%2C%0D%0A%7D%0D%0A%0D%0A

Related Issues:
Couldn't find.

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFix AvailableA PR has been opened for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions