type X1 = 100;
type X2 = 200|201;
type A<T1, T2 extends X1 | X2> = {x:T1, s:T2};
type A1 = A<{y:number}, 100> | A<{z:number}, 200>;
type A2 = A<{z:number}, 200> | A<{y:number}, 100>;
function f<T>(p:A<T, X1> | A<{z:number}, X2>) {
}
f({} as A1);
f({} as A2); //error
Next snippet
type X1 = 100;
type X2 = 200; // not union type
type A<T1, T2 extends X1 | X2> = {x:T1, s:T2};
type A1 = A<{y:number}, 100> | A<{z:number}, 200>;
type A2 = A<{z:number}, 200> | A<{y:number}, 100>;
function f<T>(p:A<T, X1> | A<{z:number}, X2>) {
}
f({} as A1);
f({} as A2); //no error
Why first constituent in
p:A<T, X1> | A<{z:number}, X2>
is fixed while infer in the first snippet, but is not in the second?
3
Yes, even though unions are not supposed to have an observable order, it is true that sometimes inference is affected by the the internal representation of the union ordering. This can happen during generic type argument inference when TypeScript finds multiple candidate types.
An example of multiple candidate types:
function foo<T>(x: { a: T }) { }
foo(Math.random() < 0.5 ? { a: "abc" } : { a: 123 }) // error!
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// function foo<string>(x: { a: string }): void;
// Argument of type '{ a: string; } | { a: number; }' is not
// assignable to parameter of type '{ a: string; }'.
foo(Math.random() < 0.5 ? { a: 456 } : { a: "def" }); // error!
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// function foo<number>(x: { a: number }): void;
// Argument of type '{ a: number; } | { a: string; }' is not
// assignable to parameter of type '{ a: number; }'.
In the above calls, TypeScript sees both number
and string
as candidates for T
, and neither one is considered to be higher priority because they both come from the same part of a union. TypeScript just chooses one of the candidates, based on whichever one it considers first. In the first call it chooses string
and in the second call it chooses number
. Neither choice works, so this is a fairly harmless consequence of the inference algorithm depending on union order, since it really just changes the wording of the error message. (Note that it is intentional that these calls fail; TypeScript will not generally synthesize the union type number | string
from {a: number} | {a: string}
because often it’s not what people want, and it’s a potential performance problem.)
In your case you have run into the unfortunate situation where the inference succeeds in one case and fails in the other. This is a known behavior of TypeScript, reported at microsoft/TypeScript#59729 and is being treated like a suggestion and not a bug. It’s possible that it will be fixed by having TypeScript try to filter a union of same-priority candidates to make it more likely to pick an applicable one. But until and unless that happens, you’ll just have to work around it.
The most common workaround: in cases where TypeScript’s generic inference doesn’t work as desired, you can always manually specify the generic type argument:
f({} as A2); // error
f<{ y: number }>({} as A2); // okay
This relieves the compiler of the duty of inferring the type, but still allows it to check the type to make sure you didn’t make a mistake.
Playground link to code