Type inference on `filterItems` when using type predicates
Author: hyunbinseoCreated Sep 5, 2026Updated Sep 5, 2026
Follow-up to #867, which added type-predicate narrowing to findItem.
filterItems still uses the plain ArrayRequirement and returns the input type.
import * as v from 'valibot';
type Animal = 'cat' | 'dog';
const isAnimal = (x: string): x is Animal => x === 'cat' || x === 'dog';
const schema = v.pipe(
v.array(v.string()),
v.filterItems(isAnimal),
);
v.parse(schema, ['apple', 'cat', 'screw']);
// inferred: string[] — want: Animal[]Desired: when the operation is a type guard (item) => item is T, type the action BaseTransformation<TInput, T[], never>, mirroring Array.prototype.filter's overload.
ArrayRequirement$1 (the guard-capable requirement from #867) already exists, so this is mostly wiring.
Workaround today:
v.pipe(v.array(v.string()), v.transform((a) => a.filter(isAnimal)));Assisted-by: Claude:claude-sonnet-5
Source: open-circle/valibot