typescripttypescript-genericstypescript-utility

Typescript - How to get keys behind specific values from an object


I have a super simple use-case. I want have a function pluckOnlyStringValues on which I pass an object obj and a key and I want to ensure that I can pass only such keys whose values are string. In such a way that pluckOnlyStringValues always returns string.

For the goal I'm trying implementing a type helper PickKeysByValue, but it does not seem to work...

type PickKeysByValue<T extends object, ValueTypes> = {
  [K in keyof T]-?: T[K] extends ValueTypes ? K : never;
}[keyof T];

// Working
type GetKeysWithStringValues = PickKeysByValue<
  { a: string; b?: string; c: number | undefined; d: () => 4 },
  string
>;
// Working
type GetStringValues = { a: string; b?: string; c: number | undefined; d: () => 4 }[GetKeysWithStringValues]

// Not working
const pluckOnlyStringValues = <O extends { a: string }>(
  obj: O,
  key: PickKeysByValue<O, string>,
): string => {
  return obj[key];
};

Solution

  • If you change the constraint of O to Record<string, any>, TypeScript will know that O is indexable with a string.

    const pluckOnlyStringValues = <O extends Record<string, any>>(
      obj: O,
      key: PickKeysByValue<O, string>,
    ): string => {
      return obj[key];
    };