-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Excess property checking only checks top level when target type is a union #21187
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
@sandersn any thoughts about what can be done here? |
@DHainzl Unfortunately, this is intentional. The excess property check only checks the top level when the target type is a union. That's because of examples like this: interface CommonA {
a: string
b: number
m1(): string
}
interface CommonB {
a: string
b: number
m2(): number
}
var both: CommonA | CommonB = {
a: 'hi'
b: 1
m1() { return this.a }
m2() { return this.b }
} To check whether the object literal is assignable to One cause of this is that union is not exclusive union. By analogy to binary operators, it's |
Later: No, this doesn't work for overlapping discriminants: type Ambiguous = {
tag: "A",
x: string
} | {
tag: "A",
y: number
} | {
tag: "B",
z: boolean
} | {
tag: "C"
}
let amb: Ambiguous
// should have no error for ambiguous tag, even when it could satisfy both constituents at once
amb = { tag: "A", x: "hi" }
amb = { tag: "A", y: 12 }
amb = { tag: "A", x: "hi", y: 12 } |
Never mind, I got the previous error because I still had a previous fix attempt in the build. I have a fix up at #21285. |
Can confirm, the main issue is now fixed with typescript@next. Thanks for the quick help! Just a side note: VSCode still shows the highlight for the
Not sure if this is something that can be fixed or will be fixed as part of this ticket? Or would you prefer that I open a new ticket for this one? |
Opening a new ticket is better. I’m not as fluent with quick info, so I need something to remind me to go look at it. I think completions might disable excess property checks in the name of providing more completions, but that doesn’t make sense for quick info. |
When using Discriminated Unions with properties with interfaces as types, TS does not seem to infer the discrimination correctly when defining a variable of that type. See the following example:
TypeScript Version: 2.7.0-dev.201xxxxx
Code
Expected behavior:
x.settings should be
FooASettings
in the type, and not deducted asFooASettings | FooBSettings
. It works correctly when using the variable, but not when defining it.Related:
I was unable to find any directly related issue for this case, the only thing I found was #12745 (comment) which might has the same root issue.
The text was updated successfully, but these errors were encountered: