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
I'm using union type expression to express the result of merging two objects. however it's being optimized in a way that changes (over-eager?) type inference behavior.
Code
I've tried to simplify it as much as I can while still keeping some 'real-world' relevance.
typeArgument<T>={[PinkeyofT]: T[P];// in the real use case it's `[P in keyof T]: SomeType<T[P]>`};classContext<T>{constructor(privatearg: Argument<T>){}get<T1extendskeyofT>(key: T1): T[T1]{returnthis.arg[key];// in the real use case it's `this.arg[key].something()`}}declarefunctionmerge2<T1,T2>(arg1: Argument<T1>,arg2: Argument<T2>): Argument<T1|T2>;leta=merge2({foo: {foobar: true}},{foo: {foobar: 4}});constres=newContext(a).get('foo').foobar;
Expected behavior:
I expect this code to compile, and res to be of type number | boolean
Actual behavior:
Compilation fails .
using a custom union type expression works, however:
// replaces T1 | T2 expression in the type of merge2's resulttypeUnion<T1,T2>={[Pinkeyof(T1|T2)]: PextendskeyofT1 ? PextendskeyofT2 ? T1[P]|T2[P]: T1[P] : PextendskeyofT2 ? T2[P] : never;}
I think the union type you had there is not correct.. Argument<T1 | T2> should be Argument<T1> | Argument<T2> since you can not have a transformation on the union, but rather a union of the transformations.
That said. the behavior of failing inference if it comes up with non-matching candidates is something that we thought about changing, but did not seem like the right move. you can find more discussion and reasoning in #20339 and #19596.
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.
I'm using union type expression to express the result of merging two objects. however it's being optimized in a way that changes (over-eager?) type inference behavior.
TypeScript Version:
3.0.0-dev.20180616 , 2.9.1-insiders.20180525
Search Terms:
union optimization inference merge
Code
I've tried to simplify it as much as I can while still keeping some 'real-world' relevance.
Expected behavior:
I expect this code to compile, and res to be of type
number | boolean
Actual behavior:
Compilation fails .
using a custom union type expression works, however:
Playground Link:
the bug
Related Issues:
#18043
The text was updated successfully, but these errors were encountered: