Order of constituents of union type is matter?

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

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật