can1357/oh-my-pi · error · OmpTypeError

cannot apply length bound to ${ir.k}

Error message

cannot apply length bound to ${ir.k}

What it means

withLengthBound applies a min/max length constraint to an intermediate representation (IR) node, but only array and string IRs support length bounds. If called with any other kind (number, object, literal, …) it throws, indicating the constraint is being applied to a type that has no length concept.

Source

Thrown at packages/omptype/src/type.ts:2463

}

function maxOf(a: number | undefined, b: number | undefined): number | undefined {
	if (a === undefined) return b;
	if (b === undefined) return a;
	return Math.max(a, b);
}

function minOf(a: number | undefined, b: number | undefined): number | undefined {
	if (a === undefined) return b;
	if (b === undefined) return a;
	return Math.min(a, b);
}

function withLengthBound(ir: IR, side: "min" | "max", bound: number): IR {
	if (ir.k === "array" || ir.k === "string") {
		return side === "min" ? { ...ir, min: bound } : { ...ir, max: bound };
	}
	throw new OmpTypeError(`cannot apply length bound to ${ir.k}`);
}

function withNumericBound(ir: IR, side: "min" | "max", bound: number, exclusive = false): IR {
	if (!Number.isFinite(bound)) throw new OmpTypeError("numeric bound must be finite");
	if (ir.k === "number") {
		return side === "min" ? { ...ir, min: bound, xmin: exclusive } : { ...ir, max: bound, xmax: exclusive };
	}
	if (ir.k === "union") {
		return { ...ir, members: ir.members.map(member => withNumericBound(member, side, bound, exclusive)) };
	}
	throw new OmpTypeError(`cannot apply numeric bound to ${ir.k}`);
}
interface GenericParameter {
	readonly name: string;
	readonly constraintDef?: unknown;
}

interface GenericMeta {

View on GitHub (pinned to 9690622007)

Solutions

  1. Apply the length constraint to a string() or array() builder instead
  2. Remove the length constraint from the non-length type
  3. If a number bound was intended, use numeric bounds (min/max on number) rather than length bounds
  4. Catch OmpTypeError to identify which IR kind received an invalid bound

Example fix

// before
number().min(3); // length-style bound intended for strings/arrays
// after
string().min(3); // or number().min(3) as a numeric bound
Defensive patterns

Strategy: validation

Validate before calling

const supportsLengthBound = (t) => t?.k === 'string' || t?.k === 'array';
// guard before applying: if (!supportsLengthBound(t)) skip or use numeric bounds

Type guard

const supportsLengthBound = (t) => t?.k === 'string' || t?.k === 'array';

Try / catch

try {
  ir = withLengthBound(ir, 'min', n);
} catch (err) {
  if (err instanceof OmpTypeError && err.message.startsWith('cannot apply length bound')) {
    // apply the appropriate bound kind for ir.k or drop the constraint
  } else throw err;
}

Prevention

When it happens

Trigger: Internally, applying .min()/.max()-style length constraints during IR construction when the builder chain is attached to a non-length type; custom builder code that calls the internal bound helper on e.g. number().

Common situations: Chaining a length constraint onto a numeric or boolean builder by mistake (wrong builder selected); a refactor changed the base type but left the length constraint in place; misuse of internal IR helpers in extension code.

Related errors


AI-assisted analysis of can1357/oh-my-pi@9690622007 (2026-08-31). Data as JSON: /api/errors/1fce5a0b2a73aced. Report an issue: GitHub.