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
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.
FilterBy<T, P>
filters keys from the object typeT
based on a specified predicateP
.