-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Excess property checking only applies to half of an intersection #30715
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
Comments
Either an EPC bug or "Not all intersection types are necessarily constructable" |
It works if you do: 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 {
// Note the union (`|`) instead of intersection (`&`)
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: [
// Note the explicit type cast / declaration:
<ValueAndKey>{
key: 'bla',
value: null,
},
],
},
};
const reproWithoutArray: BugReproWithoutArray = {
dataType: {
key: 'bla',
value: null,
},
}; Playground link: https://www.typescriptlang.org/play/#src=🔗 |
I understand. As I said I could just typecast and it will work, but it should without typecasting. |
@weswigham or @andrewbranch I thought I heard something recently from one of you about better handling of intersections wrt excess property checks. Does that sound familiar? I haven't touched this code in a while and I thought that it used to have intersection-specific code. (Maybe I'm thinking of weak type checking.) |
Probably thinking of this: #30853 |
TypeScript Version: 3.3.333
Search Terms:
Type infer array nested interface structure
Code
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.
The text was updated successfully, but these errors were encountered: