-
-
Notifications
You must be signed in to change notification settings - Fork 625
Open
Labels
Description
Type description + examples
A way to filter arrays and objects based on the value of the members/props extending and other type given similar Array.prototype.filter
, with the Boolean
interface filtering any falsy
value like filter(Boolean)
does
Type source
Just main concept rest of code in #1183 :
export type FilterType<T, U, S extends boolean> = Boolean extends U ? IsTruthy<T> : Extends<T, U, S>;
/**
Filters elements from an `Array_` based on whether they match the given `Type`.
If `Type` is `Boolean`, it filters out `falsy` values like `Boolean(T)` does.
Strict controls whether strict or loose type comparison is used (defaults to loose).
*/
export type ArrayFilter<
Array_ extends UnknownArray, Type,
Strict extends boolean = false,
> = CleanEmpty<_ArrayFilter<Array_, Type, Strict>>;
/**
Internal implementation of {@link ArrayFilter}.
Iterates through the array and includes elements in the accumulator if they pass `FilterType`.
*/
type _ArrayFilter<Array_ extends UnknownArray, Type, Strict extends boolean, Acc extends any[] = []> = (
Array_ extends [infer Head, ...infer Tail]
? FilterType<Head, Type, Strict> extends true
? _ArrayFilter<Tail, Type, Strict, [...Acc, Head]>
: _ArrayFilter<Tail, Type, Strict, Acc>
: Acc
);
Search existing types and issues first
- I tried my best to look for it