oven-sh/bun · error · Error

Cannot derive optional on a required type

Error message

Cannot derive optional on a required type

What it means

Build-time error in the bindgen type-builder DSL: `.optional` was accessed on a TypeImpl already marked required. Optional and required are mutually exclusive flags in the generated-binding schema, so the generator throws during codegen rather than emitting invalid bindings.

Solutions

  1. Remove `.required()` from the type chain before `.optional`
  2. Pick one of required/optional for the parameter
  3. Run the codegen again after fixing the type definition
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at src/codegen/bindgen-lib-internal.ts:528 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of oven-sh/bun@8c5296ac45 (2026-08-16). Data as JSON: /api/errors/2bd49858dd227d07. Report an issue: GitHub.

Appendix: source

Thrown at src/codegen/bindgen-lib-internal.ts:528

          .filter(k => this.flags[k])
          .map(x => ", " + x)
          .join("")}]`,
        "regexp",
      )}` +
      (this.data
        ? " " +
          inspect(this.data, {
            ...options,
            depth: options.depth === null ? null : options.depth - 1,
          }).replace(/\n/g, "\n")
        : "")
    );
  }

  // Public interface definition API
  get optional() {
    if (this.flags.required) {
      throw new Error("Cannot derive optional on a required type");
    }
    if (this.flags.default) {
      throw new Error("Cannot derive optional on a something with a default value (default implies optional)");
    }
    return new TypeImpl(this.kind, this.data, {
      ...this.flags,
      optional: true,
    });
  }

  get finite() {
    if (this.kind !== "f64") {
      throw new Error("finite can only be used on f64");
    }
    if (this.flags.finite) {
      throw new Error("This type already has finite set");
    }
    return new TypeImpl(this.kind, this.data, {

View on GitHub (pinned to 8c5296ac45)