fabian-hiller / valibot

The modular and type safe schema library for validating structural data 🤖
https://valibot.dev
MIT License
6k stars 186 forks source link

TypeScript: InferInput always infers intersect as 'never' #601

Closed naveen-bharathi closed 4 months ago

naveen-bharathi commented 4 months ago

For any valibot schema that uses intersect, InferInput always infers intersect as type never

For the below code, the v.InferInput infers typeof eventSchema as never

This started in the first pre-release v0.31.0-rc.0 and still happens in the latest pre-release v0.31.0-rc.4

import * as v from 'valibot'

const eventSchema = v.intersect([
  v.pipe(
    v.object({
      name: v.pipe(
        v.string(),
        v.trim(),
        v.maxLength(128),
      ),
      startDate: v.date(),
      endDate: v.date(),
    }),
    v.check(({ startDate, endDate }) => startDate < endDate),
  ),
  v.variant('hasCelebrity', [
    v.object({
      hasCelebrity: v.literal(true),
      celebrity: v.literal('Fabian Hiller'),
    }),
    v.object({
      hasCelebrity: v.literal(false),
      celebrity: v.null_(),
    }),
  ]),
])

type EventSchemaT = v.InferInput<typeof eventSchema> // infers as 'never' (all the time)

Screenshot from VSCode Intellisense

SCR-20240525-igfe
fabian-hiller commented 4 months ago

Thanks for reporting this. I will fix it and release a new version.

fabian-hiller commented 4 months ago

v0.31.0-rc.5 is available

naveen-bharathi commented 4 months ago

Many Thanks @fabian-hiller You're a super star ⭐