different-ai/openwork · error · InterpreterRuntimeError
Number.${name} expects a number argument.
Error message
Number.${name} expects a number argument. What it means
optNum validates optional numeric parameters (digits for toFixed/toPrecision, radix for toString) inside invokeNumberMethod. If the slot is provided but is not a number, the library throws this error rather than coercing, since silent coercion of e.g. digits="2" would hide bugs.
Source
Thrown at packages/codemode/src/stdlib/number.ts:11
export const numberMethods = new Set(["toFixed", "toPrecision", "toExponential", "toString"])
export const numberConstants = new Set(["MAX_SAFE_INTEGER", "MIN_SAFE_INTEGER", "MAX_VALUE", "MIN_VALUE", "EPSILON"])
export const numberStatics = new Set(["isInteger", "isFinite", "isNaN", "isSafeInteger", "parseInt", "parseFloat"])
export const invokeNumberMethod = (value: number, name: string, args: Array<unknown>, node: AstNode): unknown => {
const optNum = (index: number): number | undefined => {
const arg = args[index]
if (arg === undefined) return undefined
if (typeof arg !== "number") throw new InterpreterRuntimeError(`Number.${name} expects a number argument.`, node)
return arg
}
let result: unknown
switch (name) {
case "toFixed":
result = value.toFixed(optNum(0))
break
case "toExponential":
result = value.toExponential(optNum(0))
break
case "toPrecision": {
const digits = optNum(0)
result = digits === undefined ? value.toString() : value.toPrecision(digits)
break
}
case "toString": {
const radix = optNum(0)
if (radix !== undefined && (radix < 2 || radix > 36)) {View on GitHub (pinned to 2b7df46e8a)
Solutions
- Convert the argument with Number(x) or parseInt/parseFloat before the call.
- If the value may be absent, pass undefined explicitly instead of a string placeholder.
- Validate user-supplied formatting parameters at the boundary before invoking the method.
Example fix
// before const s = price.toFixed(input.digits) // after const s = price.toFixed(Number(input.digits ?? 2))
Defensive patterns
Strategy: type-guard
Validate before calling
const digits = typeof d === "number" ? d : Number(d)
if (d !== undefined && Number.isNaN(digits)) throw new TypeError("formatting digits must be numeric") Type guard
const isNumArg = (v: unknown): v is number => typeof v === "number"
Try / catch
try { return value.toFixed(d) } catch (e) { if (/expects a number argument/.test(String(e))) return value.toFixed(Number(d)); throw e } Prevention
- Always Number()-convert formatting parameters that come from JSON, env, or user input
- Pass undefined (not a string placeholder) when omitting optional numeric parameters
- Validate numeric options at the config boundary
When it happens
Trigger: Calling value.toFixed("2"), value.toPrecision(undefined-as-string), value.toString("16") — any call where the optional parameter is present with typeof !== "number". optNum is shared by digits and radix slots, so it fires for all of them.
Common situations: Receiving numeric parameters as strings from JSON config or query params; passing user input directly as precision; accidentally passing a string template value where a number was intended.
Related errors
- Number.parseInt expects a numeric radix.
- String.${name} expects argument ${index + 1} to be a number.
- JSON.parse expects a string.
- Math.${name} expects number arguments.
- Number.toString radix must be between 2 and 36.
AI-assisted analysis of different-ai/openwork@2b7df46e8a (2026-09-01).
Data as JSON: /api/errors/86d1d98f20afe9b0.
Report an issue: GitHub.