In TypeScript, how to get the keys of an object type whose values are of a given type? In TypeScript, how to get the keys of an object type whose values are of a given type? typescript typescript

In TypeScript, how to get the keys of an object type whose values are of a given type?


This can be done with conditional types and indexed access types, like this:

type KeysMatching<T, V> = {[K in keyof T]-?: T[K] extends V ? K : never}[keyof T];

and then you pull out the keys whose properties match string like this:

const key: KeysMatching<Thing, string> = 'other'; // ERROR!// '"other"' is not assignable to type '"id"'

In detail:

KeysMatching<Thing, string> ➡{[K in keyof Thing]-?: Thing[K] extends string ? K : never}[keyof Thing] ➡{   id: string extends string ? 'id' : never;   price: number extends string ? 'number' : never;  other: { stuff: boolean } extends string ? 'other' : never;}['id'|'price'|'other'] ➡{ id: 'id', price: never, other: never }['id' | 'price' | 'other'] ➡'id' | never | never ➡'id'

Note that what you were doing:

type SetNonStringToNever<T> = { [P in keyof T]: T[P] extends string ? T[P] : never };

was really just turning non-string property values into never property values. It wasn't touching the keys. Your Thing would become {id: string, price: never, other: never}. And the keys of that are the same as the keys of Thing. The main difference with that and KeysMatching is that you should be selecting keys, not values (so P and not T[P]).

Playground link to code


As a supplementary answer:

Since version 4.1 you can leverage key remapping for an alternative solution (note that core logic does not differ from jcalz's answer). Simply filter out keys that, when used to index the source type, do not produce a type assignable to the target type and extract the union of remaining keys with keyof:

type KeysWithValsOfType<T,V> = keyof { [ P in keyof T as T[P] extends V ? P : never ] : P };interface Thing {    id: string;    price: number;    test: number;    other: { stuff: boolean };}type keys1 = KeysWithValsOfType<Thing, string>; //id -> oktype keys2 = KeysWithValsOfType<Thing, number>; //price|test -> ok

Playground


As rightfully mentioned by Michal Minich:

Both can extract the union of string keys. Yet, when they should be used in more complex situation - like T extends Keys...<T, X> then TS is not able to "understand" your solution well.

Because the type above does not index with keyof T and instead uses keyof of the mapped type, the compiler cannot infer that T is indexable by the output union. To ensure the compiler about that, one can intersect the latter with keyof T:

type KeysWithValsOfType<T,V> = keyof { [ P in keyof T as T[P] extends V ? P : never ] : P } & keyof T;function getNumValueC<T, K extends KeysWithValsOfType<T, number>>(thing: T, key: K) {    return thing[key]; //OK}

Updated Playground