How to properly use generics with conditional types in TypeScript to handle multiple keys?

I have a TypeScript function useSome that uses generics to define a params object based on a given key or set of keys:

const useSome = <Keys extends string | undefined>() => {
  const params = {} as Keys extends string ? Record<Keys, string> : Partial<Record<string, string>>;

  return params;
};

Here’s how I want to use this function:

  1. Single Key Example:
const { id } = useSome(); // Expected: id is of type string | undefined
  1. Multiple Keys Example:
const { id, type } = useSome<'id' | 'type'>(); // Expected: id and type are both of type string

However, when I provide multiple keys in the generic, I get the following error:

Property 'id' does not exist on type 'Record<"id", string> | Record<"type", string>'.

Here’s a TypeScript’s Playground link to demonstrate the issue.

My Questions:

How can I modify the useSome function so that it works correctly with multiple keys?
Is it possible to achieve the desired behavior when passing multiple keys in the generic type?

2

Caveat: It’s not clear to me why you’d want useSome<'id'>() to produce a value of type {id: string} and not {id?: string}, since at runtime you’re just calling useSome() and there’s no possible way to guarantee that the keys a TS developer specifies will actually exist. It’s effectively like using a type assertion, but hidden inside the implementation of your function. If you’re going to do that, I’d recommend just having people write useSome() as {id: string} and be explicit about the lack of type safety. From here on out, though, I’ll assume you have a reason why useSome() should produce {[k: string]: string | undefined} but useSome<'id'> should produce {id: string}.


My inclination would be to define your types like this:

const useSome = <K extends string>() => {
  const params = {} as string extends K ? { [P in K]?: string } : { [P in K]: string }    
  return params;
}

If you call useSome() without manually specifying the generic type argument for K, TypeScript will fail to infer it, and it will fall back to the constraint of string. On the other hand if you call useSome() and specify a string literal type or a union of such types, like useSome<'x'|'y'>(), then K will be narrower than string.

Thus we can just check if K is or is not narrower than string. We know that K extends string, but we need the opposite check. The conditional type string extends K ? ⋯ : ⋯ will be true if K is exactly string (or wider, but it can’t be wider by the constraint) and false if K is some string literal type or union of such types.

So if string extends K is true then we want optional properties as in {[P in K]?: string}, which evaluates to {[k: string]: string | undefined} (since a mapped type over string produces a string index signature and the ? mapping modifier just adds undefined to the index signature property, since there are no optional index signatures). Otherwise we want required properties as in {[P in K]: string}.

Let’s check it out:

const { id, type } = useSome<'id' | 'type'>()
//      ^? const id: string
/* const useSome: <"id" | "type">() => {
    id: string;
    type: string;
} */

const { id2 } = useSome()
//      ^? const id2: string | undefined
/* const useSome: <string>() => {
    [x: string]: string | undefined;
}*/

Looks good.

Playground link to code

If I understand your question correctly, you want the useSome function to return an object with properties based on the provided generic type arguments and to correctly handle multiple generic keys.

Try this:

const useSome = <Keys extends string | undefined>(...keys: Keys[]): Record<Extract<Keys, string>, string> => {
  const params = {} as Record<Extract<Keys, string>, string>;

  // Example of how you might populate `params` with some values.

  keys.forEach(key => {
    params[key] = "some value";
  });

  return params;
}

// Usage examples
const { id } = useSome('id'); // type of id is string

const { id, type } = useSome('id', 'type'); // type of id is string, type of type is string

Hope it helps!

1

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