Type Alias ElementMatch<T, Criteria, Options>

ElementMatch<T, Criteria, Options>: [T] extends [Criteria]
    ? T
    : TypePlusOptions.Merge<Options, ArrayPlus.ElementMatch.DefaultOptions<Criteria>> extends infer C extends Record<keyof ArrayPlus.ElementMatch.Options, unknown>
        ? (T extends Criteria
                ? T
                : C["widen"] extends true
                    ? Criteria extends T
                        ? C["$widen"]
                        : C["$notMatch"]
                    : C["$notMatch"]) extends infer R
            ? IsUnion<T, IsNever<R, {
                $else: R | C["$unionNotMatch"];
                $then: R;
            }>, R>
            : C["$notMatch"]
        : never

🦴 utilities 🔢 customizable

Filter the element T in an array or tuple to match Criteria.

Type Parameters

Allow using narrow type to match widen type. e.g. number, 1 -> 1 | undefined. Default to true.

Return value when T does not match Criteria. Default to never.

Return value when widen is true. Default to Criteria | undefined.

Return value when a branch of the union T does not match Criteria. Default to never.

If you want the type to behave more like JavaScript, you can override it to return undefined.

Since it is a union, the result will be joined to the matched branch as union. e.g. ElementMatch<1 | 2, 1> -> 1 | undefined