Why TS generates wrong type after infer and template literal type

Why does the code below output the wrong type?


type Excuse<Excuses extends Record<string, string>> = {
  new (excuses: Excuses): keyof Excuses extends `${infer Key}`
    ? `${Key}: ${Excuses[Key]}`
    : never;
};

const helpingTheReindeer = {
  helping: 'the reindeer',
  differentKey: 'hello'
} as const;

declare const Excuse0: Excuse<typeof helpingTheReindeer>;
const excuse0 = new Excuse0({
  ...helpingTheReindeer,
});
type t0_actual = typeof excuse0;

As we can see t0_actual is "helping: the reindeer" | "helping: hello" | "differentKey: the reindeer" | "differentKey: hello" and not just "helping: the reindeer" | "differentKey: hello". Why?

I asked this question to ChatGpt and it said that TS tries to combine all keys with all values. But why is that? The object is passed as a constant, its value cannot change

You can try it here Playground link

I was solving https://www.adventofts.com/ day 11 and discovered this. I’m waiting for an explanation of why the TS type system behaves the way it does

New contributor

Ermolaev.ID is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

3

The type keyof Excuses extends `${infer Key}` ? `${Key}: ${Excuses[Key]}` : never; uses inferring within conditional types to essentially “copy” the union type keyof Excuses (which is "helping" | "differentKey") into the type variable Key (you use a template literal type there but it doesn’t do much in this circumstance). So Key is just a union, and when you mention it twice inside `${Key}: ${Excuses[Key]}`, you get the single type `${"helping" | "differentKey"}: ${Excuses["helping" | "differentKey"]}`, and any correlation between "helping" in the first mention of Key and "helping" in the second mention of Key is lost. Thus it expands out to the four element union you don’t want.


What you want to do is distribute the `${Key}: ${Excuses[Key]}` type over unions in Key. So that if Key is A | B then your result is `${A}: ${Excuses[A]}` | `${B}: ${Excuses[B]}`. There are various ways to get this to happen. You could use a distributive conditional type, wrapping `${Key}: ${Excuses[Key]}` with an apparently no-op Key extends unknown ? ⋯ : never:

type Excuse<Excuses extends Record<string, string>> = {
  new(excuses: Excuses): keyof Excuses extends `${infer Key}` ?
    Key extends unknown ? `${Key}: ${Excuses[Key]}` : never
    : never;
};
// type t0_actual = "helping: the reindeer" | "differentKey: hello"

Or, since Key is keylike to begin with, you can use a distributive object type as coined in microsoft/TypeScript#47109, where you make a mapped type over all the keys and then index into it with those keys:

type Excuse<Excuses extends Record<string, string>> = {
  new(excuses: Excuses): keyof Excuses extends `${infer Key}` ?
    { [P in Key]: `${P}: ${Excuses[P]}` }[Key]
    : never;
};
// type t0_actual = "helping: the reindeer" | "differentKey: hello"

I tend to prefer distributive object types because TypeScript “understands” them better. In the above, I probably wouldn’t bother with a conditional type at all, since instead of copying keyof Excuses into Key, I’d just map over keyof Excuses directly:

type Excuse<X extends Record<string, string>> = {
  new(excuses: X): { [Key in keyof X]:
    `${string & Key}: ${X[Key]}`
  }[keyof X];
};
// type t0_actual = "helping: the reindeer" | "differentKey: hello"

And the only thing I needed to change was to intersect Key with string inside the template literal type, since keyof X might have symbols (no, X extends Record<string, string> does not prevent symbol-valued keys), and you can’t serialize those in template literals.

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