-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Type union not matched starting from TS 5.1 #58603
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
Why isn't type TypeB = {
kind: MyEnum.B;
} & ({ id?: number | undefined; }); |
As mentioned above, in the real world example were we encounter this, the "id: number" type branch can have additional fields. While when not providing an id (or undefined), you should not provide these. E.g. type TypeB = {
kind: MyEnum.B;
}
& (
{
id?: undefined;
}
|
{
id: number;
commentId? number;
}
); |
You canβt actually prevent a |
Bisects to #53709 |
Yeah, there's an issue here. This is a simplified repro: type Foo = { kind: "a" | "b", value: number } | { kind: "a", value: undefined } | { kind: "b", value: undefined };
function test(obj: { kind: "a" | "b", value: number | undefined }) {
let x1: Foo = obj; // Ok
let x2: Foo = { kind: obj.kind, value: obj.value }; // Error, but shouldn't be
} Above, our type discrimination logic reduces type |
π Search Terms
"5.1", "type union"
π Version & Regression Information
β― Playground Link
https://www.typescriptlang.org/play/?ts=5.5.0-beta#code/LAKApgdgrgtgBAWQJ4FFrwN6gJAEE4C8cAjADQ4BChcATKAL6igAuSADmHACrtj5FYQ2ANYBLCABMAXIlToAdLgDcOURID8M9ACMwAJxUh6hlr268qAnGMkzkaWPIqHGIAGRwAFDkHZsazTgoSTAAM3EwCUNsV2wAHx8cf2k4HX1o1wBKExBWDlkefKJCvjg48w5nJhBQ4IBjZlEAewg4AGUmmDBmAAtxAHNPAEM7JBLMmQA3JrU4DFdQDq7egc8MOBsU+wV8emzFzu6+iEH1zdGHGCc4PcMlo9Wz8S25R1xSODUZYIkwiIkbvsQPcVic1htnhcdh8viRAXdDqDThDbLJLk4YSkfn8IJF4Qdlsdkec0QoKJiZMR8SBQLUIA1mq0AJKSUR6MANTwk7awTGBNJ6CZwaazXwgolrJKbchCZI4W4Map0hktOAsiRsjnMGhcyGk3mfDRaWC6QVTGYA3wAZwA7qJmHUel5Npk5kk6kMrZweVdcFJ3Z7va8rhR-bLsOLHkk-NLo3Lwwrw9p2UNhNFsL9QkMoAAbZhhvzYZNgVMZRU0mr1Rqq9WahoAZl1qJ9fONMFNQpFlpwtvtjudz1dvmwHq9+t9Bb8kbBw5jzxlhfjhcTheLpeqflHQfRoej09Ocdj4aXfhXfjXaaSmezecnRZTl6ErnoQA
π» Code
π Actual behavior
Indirect and Indirect2 report an error, while Indirect3 does not even though the call signature is identical (there is just an extra type check)
π Expected behavior
All 3 versions of Indirect behave the same and produce no errors
Additional information about the issue
We are currently on 4.9 and are preparing to migrate to 5.5 once it goes GA.
In our codebase TypeB has some additional required fields when id is a numer, hence the union.
I did observe that a simpler call signature (using only the discriminator) also fails in 4.9 and 5.0
The text was updated successfully, but these errors were encountered: