Assuming you intend Test
to be this:
type Test = {
includeMe: 'a',
andMe: 'a',
butNotMe: 'b',
orMe: 'b'
};
and assuming that you want PickByValue<T, V>
to give all the properties which are subtypes of V
(so that PickByValue<T, unknown>
should be T
), then you can define PickByValue
like this:
type PickByValue<T, V> = Pick<T, { [K in keyof T]: T[K] extends V ? K : never }[keyof T]>
type TestA = PickByValue<Test, 'a'>; // {includeMe: "a"; andMe: "a"}
type IncludedKeys = keyof PickByValue<Test, 'a'>; // "includeMe" | "andMe"
But if all you want is IncludedKeys
, then you can do that more directly with KeysMatching<T, V>
:
type KeysMatching<T, V> = {[K in keyof T]: T[K] extends V ? K : never}[keyof T];
type IncludedKeysDirect = KeysMatching<Test, 'a'> // "includeMe" | "andMe"