fabian-hiller / valibot

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

TS2589: Type instantiation is excessively deep and possibly infinite using forward and check #708

Closed citrocitrusyolo closed 3 months ago

citrocitrusyolo commented 3 months ago

With the below schema we're receiving a TS error TS2589: Type instantiation is excessively deep and possibly infinite.

Code:

import * as v from 'valibot';

export const changePinSchema = v.pipe(
  v.object({
    pin: v.pipe(
      v.string('Enter 4-digit numerical PIN'),
      v.minLength(1, 'PIN is required'),
      v.length(4, 'Invalid PIN format. Please enter a 4-digit numerical PIN.'),
    ),
    confirmPin: v.string('Confirm your 4-digit PIN'),
  }),
  // @ts-expect-error TS2589 - see https://github.com/microsoft/TypeScript/issues/53514
  v.forward(
    v.check(
      ({ pin, confirmPin }) => pin === confirmPin,
      'PIN mismatch. Please verify your entry.',
    ),
    ['confirmPin'],
  ),
);

export type ChangePinFields = v.InferOutput<typeof changePinSchema>;

Lib:

"valibot": "^0.35.0",
"typescript": "^5.1.3"

Unfortunately even if we ts-expect it our ts run time has doubled, and ignoring this file means we need to also ignore all importers of it. for now our workaround is to handle that check manually in the component form rather than valibot which is a bummer bc this is super helpful lib

fabian-hiller commented 3 months ago

Could this problem be related to your TypeScript version? Everything looks fine in my editor and in our online playground.

citrocitrusyolo commented 3 months ago

thank you mate! that was it, you're a good egg. thanks so much for the quick response