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

  1. Convert the argument with Number(x) or parseInt/parseFloat before the call.
  2. If the value may be absent, pass undefined explicitly instead of a string placeholder.
  3. 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

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


AI-assisted analysis of different-ai/openwork@2b7df46e8a (2026-09-01). Data as JSON: /api/errors/86d1d98f20afe9b0. Report an issue: GitHub.