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];
};
Oto{ a: string }? You should replace it withRecord<string, any>orRecord<string, any> & { a: string }to indicate thatobjcan be indexed with astring.