Consider this scenario:
interface Interface {
a: number
b: number
c: string
d: string
e: number[]
}
If I want to retrieve the keys of Interface
based on the type of their corresponding values in Interface
, how can I achieve that? This is similar to the functionality of Pick<T, K>
, but instead of selecting keys based on their type, we are interested in keys whose values match a certain type.
An example of what I am trying to accomplish is shown below:
type KeyOfWhereValueMatches<T, U extends U[keyof U]> = // ???
KeyOfWhereValueMatches<Interface, number> // 'a' | 'b'
KeyOfWhereValueMatches<Interface, string> // 'c' | 'd'
KeyOfWhereValueMatches<Interface, number[]> // 'e'