TypeScript: implementing Mapped Types when Reducing Object Keys with Conditional Types
I've been banging my head against this for hours. Quick question that's been bugging me - I'm trying to create a mapped type in TypeScript that reduces the keys of an object based on certain conditions, but I'm running into unexpected issues. My goal is to create a utility type that filters out keys from an object type based on their value type. Here's what I've attempted so far: ```typescript type FilterByValueType<T, V> = { [K in keyof T as T[K] extends V ? K : never]: T[K]; }; interface Example { name: string; age: number; isActive: boolean; } type OnlyStrings = FilterByValueType<Example, string>; const example: OnlyStrings = { name: 'Alice' }; ``` When I use the `OnlyStrings` type, I expect it to only allow the `name` key from the `Example` interface. However, TypeScript throws the following behavior: ``` Type '{ name: string; }' is not assignable to type 'Pick<Example, "name">'. ``` I've double-checked the conditions in my mapped type, and they appear to be correct. I also tried changing the `as` clause to ensure it's handling the conditional type properly, but the behavior continues. Is there an scenario with how I'm using mapped types and conditional types together? Any suggestions on how to resolve this would be greatly appreciated. I'm using TypeScript version 4.5.4. Am I missing something obvious? This is part of a larger service I'm building. Is there a better approach?