You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
interface union type casting creates invalid state #20896
Expected behavior:
if you would try to create such an object, you would normally get this error:
constau: AU={typeA: "A1",typeB: "B2"};/*Type '{ typeA: "A1"; typeB: "B2"; }' is not assignable to type 'AU'. Type '{ typeA: "A1"; typeB: "B2"; }' is not assignable to type 'A2'. Types of property 'typeA' are incompatible. Type '"A1"' is not assignable to type '"A2"'.*/
The problem is that you are using type AU as the parameters to foo where you do the assignment, so it is impossible to know if the assignment is truly valid.
Change foo to:
function foo(obj1: A1, obj2: A2): AU {
const obj3: AU = <AU>{
typeA: obj1.typeA,
typeB: obj2.typeB
};
return obj3;
}
Basically, any claim that this is an issue boils down to one question:
Given:
type Foo = "foo" | "bar"
Is 1 variable of type Foo assignable to another of type Foo. I think the answer needs to be yes, so your sample behaves correctly, and not catching this error is just a limitation of what the type system can do.
// this thing// vvvvconstobj3: AU=<AU>{typeA: obj1.typeA,typeB: obj2.typeB};returnobj3;
You're basically telling the compiler "I know what I'm doing". While we try to have the type system do some basic checks before you can convince it, I don't thik there's a ton we can do here.
I realize you are basing this issue on a fix I suggested yesterday, but if you read up on the context that I gave, you'll see how this construct doesn't generalize past the case where only a single-property differs. My advice was to step around the type system for when you know better in that case.
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.
Activity
sylvanaar commentedon Dec 26, 2017
The problem is that you are using type
AUas the parameters tofoowhere you do the assignment, so it is impossible to know if the assignment is truly valid.Change
footo:Basically, any claim that this is an issue boils down to one question:
Given:
type Foo = "foo" | "bar"Is 1 variable of type Foo assignable to another of type Foo. I think the answer needs to be yes, so your sample behaves correctly, and not catching this error is just a limitation of what the type system can do.
DanielRosenwasser commentedon Dec 26, 2017
But you used a type assertion!
You're basically telling the compiler "I know what I'm doing". While we try to have the type system do some basic checks before you can convince it, I don't thik there's a ton we can do here.
DanielRosenwasser commentedon Dec 26, 2017
I realize you are basing this issue on a fix I suggested yesterday, but if you read up on the context that I gave, you'll see how this construct doesn't generalize past the case where only a single-property differs. My advice was to step around the type system for when you know better in that case.
typescript-bot commentedon Jan 9, 2018
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.