Skip to content

Built-in RangeErrors whose message the program cannot read ​

Status: accepted · Date: 2026-11-29

Step 12 of the pure TypeScript plan needs a model of errors that built-in operations throw. Bigint ** throws a RangeError when its exponent is negative. BigInt(n) throws one when n is not an integer, d.toISOString() throws one for an invalid date, and padStart throws one when the result would be too long. ECMA-262 names the error's class, but each engine chooses its message. Until now the interpreter gave no value in these cases, so the translator had to refuse every operand that could reach them.

The decision ​

  • A thrown value of the interpreter can be a RangeError with no properties: Eval.rangeError, the value .error "RangeError" []. Because it has no message property, a program that reads the message has no value in the model. That matches the source, whose message depends on the engine.
  • In Lean, JSError becomes an inductive with two constructors. mk m is the error of new Error(m), as before, and rangeError is a built-in RangeError. Existing output still writes JSError.mk m, so it does not change. A file with error classes keeps Thrown.error of a JSError, so a RangeError needs no new member there.
  • An operation that can throw a RangeError is accepted only as the whole value of a declaration or of a return. There the interpreter's evalTop can give a thrown value. Inside a larger expression it would need exceptions in eval itself, which the model does not have.
  • The output tests the condition before the statement and throws JSError.rangeError when it holds. In the branch that throws, the output first evaluates the operation with let _ := …, as the Go panics record does for division. This makes the output fail wherever an operand of the source fails. Lean drops the unused let.
  • A function that contains such a statement can throw, in the interpreter's Stmt.hasThrow and in the translator's stmtThrows. So it returns Except, and calls of it hoist like calls of any other throwing function.
  • Certificates encode a thrown JSError with IR.errorVal, which maps each constructor to its interpreter value. The old encoder read the message field, and a RangeError has none.
  • A handler catches a RangeError like any other modeled error. A protected body may hold bigint ** as the whole value of a declaration or a return.

The first operation is bigint a ** b whose exponent is not a literal. program_sound covers both the declaration and the return. The proof shows that the output throws exactly when b is negative, and otherwise gives a ^ b.toNat.

The second operation is BigInt(n) on a number. JSNumber.toBigInt? n is toInt n when Number.isInteger(n), and none otherwise. The output throws when it is none. The proof needs no new number law, because toBigInt? is defined from isInteger and toInt, which the model already has. What the definition claims about the engine is that toInt is exact on an integral double, even far past the safe range. Lean cannot check that, so interpreter.test.ts compares the interpreter with Node on fractions, NaN, the infinities and integral doubles up to 1.5e300.

What is left out ​

d.toISOString() uses the model too. JSDate.toISOString writes the Date Time String Format, with a sign and six digits for a year outside 0 to 9999. The output tests JSDate.isInvalid first and throws the RangeError then. interpreter.test.ts compares the text with Node at the ends of the time range, around the four-digit years, and on random times.

JSON.stringify of a date needs no RangeError, because Date.prototype.toJSON writes null for an invalid date instead of calling toISOString. The output passes the date to the opaque JSString.json, as it does for the other types whose Lean value fixes the text. A JSDate holds only its time value, and that value fixes the text, so the translator accepts a date there.

Padding with a length that is not a literal still stays refused.

  • padStart and padEnd with a dynamic length throw only when the result exceeds the engine's string limit. That limit differs between engines, so the condition is not one test that the output can write.

A handler may read e.name of its binding and test e instanceof RangeError. The interpreter binds the thrown value in the handler. Its name is "RangeError" for a built-in error, from RangeError.prototype, and "Error" otherwise. The translator refuses a declaration named RangeError, so no other error has that class. e.message stays refused, because the engine chooses the message of a RangeError, and the model has none.

Alternatives ​

  • A message chosen by Tolean. The output could throw JSError.mk "RangeError". Then a certificate would claim a message that Node does not produce, and a program could not tell this error from new Error("RangeError").
  • A message as a hypothesis. The model could take each engine's message as a parameter. Nothing in the subset reads the message, so the parameter would add binders to every theorem and prove nothing more.
  • Exceptions inside eval. That would accept (a ** b) + 1n, but every expression lemma would have to handle a thrown value. The restriction to whole statements costs a temporary name in the source instead.

This page is docs/decisions/2026-11-29-built-in-range-errors.md at commit 5f1d031.