Skip to content

Type inference on findItem when using type predicates #867

@cberthou

Description

@cberthou

For now, findItem returns the input type event when trying to narrow the typing using type predicates. For example :

import * as v from 'valibot';

type Animal = 'cat' | 'dog';

const isAnimal = (v: string): v is Animal => v === 'cat' || v === 'dog';

const schema = v.pipe(
  v.array(v.string()),
  v.findItem(isAnimal)
)

const animal = v.parse(schema, ['apple', 'cat', 'screw']);
// animal is type string | undefined

It would be cool to get better type narrowing. For now, you have to do this, which feels weird given that findItem exists :

const schema = v.pipe(
  v.array(v.string()),
  v.transform((array) => array.find(isAnimal)),
)

Metadata

Metadata

Assignees

Labels

enhancementNew feature or requestpriorityThis has priority

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions