-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Excess property checking only applies to half of an intersection #30715
Copy link
Copy link
Closed
Labels
BugA bug in TypeScriptA bug in TypeScriptFix AvailableA PR has been opened for this issueA PR has been opened for this issue
Milestone
Description
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.
Related Issues:
Couldn't find.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
BugA bug in TypeScriptA bug in TypeScriptFix AvailableA PR has been opened for this issueA PR has been opened for this issue