Type alias FilterBy<T, P>

FilterBy<T, P>: {
    [K in Keys<T>]: K extends P
        ? K
        : never
}[Keys<T>]

FilterBy<T, P> filters keys from the object type T based on a specified predicate P.

Type Parameters

  • T
  • P

Remark

This type performs a shallow filtering of keys within T and does not check deeply nested types or complex structures within the object type.

It does not return the key as a whole object, it just returns the key itself

Example

type T = {
a: () => 1;
x: string;
s: {
q: Nullable;
s: {
i: {
x: {
o: boolean;
n: Falsy;
};
e: 'foo';
};
};
};
};
type _ = FilterBy<T, 'a'>

Results in the type 'a', which includes only the key 'a' from T.

type _ = FilterBy<T, Falsy>

Results in the type never, indicating that no keys in T match the type Falsy.

type _ = FilterBy<T, string>

Results in the type 'a' | 'x' | 's', which includes all top-level keys of T that are of type string. It did not pick up on 'e' as it is nested down.