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
typeA_or_C_Child=A|C_Child;interfaceA{a: string;}interfaceB{b: string;}interfaceC{c: string;}interfaceC_ChildextendsA,C{extra: string;}declarefunctionisA(it: any): it is A;declarefunctionisB(it: any): it is B;declarefunctionisC(it: any): it is C;declarefunctionisA_or_C_Child(it: any): it is A_or_C_Child;letitem: any;if(isA_or_C_Child(item)&&!isC(item)&&isB(item)){item;// inferred as A & B}if(isA_or_C_Child(item)&&isB(item)&&!isC(item)){item;// inferred as A_or_C_Child & B}
Expected behavior:
Within the the last two if statements, item should be inferred to be the same type, as both if statements use the same type guards &&ed together.
Actual behavior: item is (correctly, I think) inferred as A & B in the first if, but as A_or_C_Child & B in the second.
The text was updated successfully, but these errors were encountered:
The way narrowing works is by removing types from a union. in some cases, where there are two constraints that are not assignable, an intersection is created, e.g. isA(item) && isB(item).
the first clause isA_or_C_Child(item) narrows the type from any to A | C, !isC() removes the C from the union, then isB() adds the B.
the second one, isA_or_C_Child(item) narrows the type from any to A | C, then we add in the B because of isB(item). then the !isC(item) happens, and at this point we have (A | C ) & B, so trying to remove C from that results in no change.
I suppose this should be fixed by going deep on the intersection to see if we can remove one of the constituents. though it is not clear how this work in more complex situations.
TypeScript Version: nightly (2.1.0-dev.20161006)
Code
Expected behavior:
Within the the last two if statements, item should be inferred to be the same type, as both if statements use the same type guards
&&
ed together.Actual behavior:
item
is (correctly, I think) inferred asA & B
in the first if, but asA_or_C_Child & B
in the second.The text was updated successfully, but these errors were encountered: