Appearance
A JS string is a list of UTF-16 code units
Status: accepted · Date: 2026-11-06
Tolean translates a TypeScript string to JSString, a structure around List UInt16, and not to Lean's String. A literal such as "abc" becomes JSString.ofString "abc", which encodes the text as UTF-16. The model is in tolean/TypeScript/Spec/String.lean. The interpreter that defines the meaning of the subset uses it too, so a certificate compares two values of the same type.
Why not Lean's String
A JavaScript string is a sequence of UTF-16 code units (ECMA-262 §6.1.4), and it can hold a lone surrogate. A Lean String is a sequence of Unicode scalar values, so it cannot hold a lone surrogate. It also measures and orders text in a different way. For example, U+FFFF sorts after U+10000 in JavaScript, because U+10000 starts with the code unit 0xD800. In Lean it sorts before it. And "😀".length is 2 in JavaScript but 1 in Lean.
The mapping of string to String was exact only because the subset had no string operation except ===. Each string operation that the pure TypeScript plan adds would break it. A certificate was not exact either. It proves a claim for every Lean value of the parameter's type, and a JavaScript caller can pass a string that no Lean String can hold.
JSString is the definition in the spec, so the model is exact for every string, lone surrogates included. Each string operation will become an ordinary function on the list, written from the algorithm in the spec, and a proof uses the lemmas about List.
The other models
We considered two other models and rejected both:
- Keep
Stringand refuse the operations whose result differs. Whether<orlengthdiffers depends on whether a value holds a character above U+FFFF. The translator cannot know that when it translates, so it would have to approximate. - An abstract type with laws, as
numberhas.numberis abstract because an exact model of doubles is hard. An exact model of strings is easy, and a concrete model with proved lemmas needs no laws to test.
Why now
The switch changes the type of every string in the output, so every proof that a project writes about a string must change with it. Today a string can only appear in fields and literals and be compared with ===. So the switch breaks almost no proof now. Each project that writes proofs over String would make it more costly later.
What it costs
The generated Lean is less readable, because each literal carries JSString.ofString. A theorem about a string input now covers every list of code units, lone surrogates included. That is correct, because a JavaScript caller can pass such a string. A certificate that runs string code in the kernel is slower on a list of code units than on a String.
How a proof uses it
JSString.ofString_inj states that two literals give the same string exactly when they are the same text, and simp uses it. So a claim can state a literal as a Lean String, and simp can tell two literals apart. The kernel reduces JSString.ofString "abc" to its code units, so rfl and decide work on literals too. A certificate relies on both. A match cannot test a value against ofString "abc", because that is not a constructor. So a certificate's decoder reads a tag with a chain of tests such as if s = JSString.ofString "abc", which simp and rfl can both decide.
The lexer still refuses a literal that holds a lone surrogate, such as "\uD800", because the syntax tree keeps a literal's text as a Lean String. A later step can let the syntax tree hold code units, and print such a literal as its list of code units.
This page is docs/decisions/2026-11-06-jsstring.md at commit 5f1d031.
