Dynamic type fails when used inside "partial" helper

Author: jakubwolnyCreated Mar 2, 2025Updated Sep 1, 2025

When dynamic type is used inside partial then it receives also undefined value for validation

import { assert, object, partial, dynamic, literal } from "superstruct";

const User = object({
  kind: literal("user"),
});

const Bot = object({
  kind: literal("bot"),
});

const userOrBot = dynamic((value) => {
  return value.kind === "user" ? User : Bot;
});

const partialDataType = partial(
  object({
    user: userOrBot,
  })
);

try {
  assert({}, partialDataType);
} catch (e) {
  // Error: Cannot read properties of undefined (reading 'kind')
}

I created a sandbox code to prove it: https://codesandbox.io/p/devbox/wqh8pv?file=%2Findex.js

In my opinion this is not expected behaviour since dynamic type definition has no way of knowing in what context it was used, "undefined" value should simply work in this case, because it was allowed in partial definition.

Source: ianstormtaylor/superstruct