valibot: Runtime behavior of `optional`/`undefinedable` doesn’t match TS with exactOptionalPropertyTypes

#385 was resolved by adding v.undefinedable with different typing from v.optional:

import * as v from "valibot";

const optional_schema = v.object({ field: v.optional(v.number()) });
type OptionalOutput = v.InferOutput<typeof optional_schema>;
// { field?: number }

const undefinedable_schema = v.object({ field: v.undefinedable(v.number()) });
type UndefinedableOutput = v.InferOutput<typeof undefinedable_schema>;
// { field: number | undefined }

However, this typing is inconsistent with the runtime behavior: at runtime, both schemas accept and return both of {} and { field: undefined }. This means that both schemas break type safety with tsc --strict --exactOptionalPropertyTypes.

const a = v.parse(optional_schema, { field: undefined });
if ("field" in a) { // narrows a.field to string
  const n: number = a.field; // oops, this is undefined
}

const b = v.parse(
  v.union([undefinedable_schema, v.object({ bogus: v.string() })]),
  {}
);
if (!("field" in b)) { // narrows b to { bogus: string }
  const s: string = b.bogus; // oops, this is undefined
}

Either the types should be adjusted to match the runtime behavior, or the runtime behavior should be adjusted to match the types.

Zod does the former:

import { z } from "zod";

const optional_schema = z.object({ field: z.optional(z.number()) });
type OptionalOutput = z.output<typeof optional_schema>;
// { field?: number | undefined }

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 20 (20 by maintainers)

Most upvoted comments

I provided such scenarios in my original report. On both of the lines with a comment // oops, this is undefined, TypeScript infers that a variable is a string or a number when it is actually undefined at runtime.

A bug like this can result in an arbitrary wrong type being assigned to any value:

import * as v from "valibot";

function cast<T, U>(t: T): U {
  const a = v.parse(v.object({ b: v.optional(v.never()) }), { b: undefined });
  while (!("b" in a)) {}
  return a.b ?? t;
}

const n: number = cast("twenty-too");
console.log(n);
$ tsc --module Node16 --strict --exactOptionalPropertyTypes test.ts
$ node test.js
twenty-too

Please appreciate that a type safety violation is an actual problem. In a project as large as Zulip, it is not possible for the core reviewers and hundreds of new open-source contributors to keep the entire codebase in our heads, and manually account for subtle discrepancies between the compiler’s reported types and the actual types, so it is very easy for those discrepancies to snowball into major bugs and security vulnerabilities. We need to be able to rely on TypeScript’s correct checking, and TypeScript needs to be able rely on libraries’ correct type definitions in order to provide that.

Yes, I read #385; it’s the very first thing I linked in my original report. I’m pretty sure the reporter doesn’t realize how unsafe the situation has become (“I can now return the validation result directly and don’t have to check for those undefined values anymore”).

Thank you very much for your time and feedback! I agree and will be reviewing and re-evaluating the situation over the next few weeks. I will share my findings with you so that we can discuss the final decision together.