Skip to content

String-keyed records use ordered association lists ​

Record<string, V> uses an ordered association list rather than a structure with a fixed set of fields. Named types, inline types and Readonly forms share this representation. Static object literals supply the properties. A lookup has type V | undefined, as it does under noUncheckedIndexedAccess.

Lookup compares UTF-16 code units without decoding the key. This matters because a lone surrogate must not turn into a replacement character and accidentally find a different property.

Lookup certificates ​

The certificate generator accepts a dynamic lookup only when a direct Object.hasOwn(record, key) test guards a read of that same record and key. The generated link theorem also has a premise that the IR run succeeds. That premise rules out dynamic reads that resolve to an inherited Object.prototype property, which the record model cannot represent. Certification also supports a local guard inside an array callback or loop body. The per-element callback proof and loop-step proof derive ownership from the true branch of that guard. Their helper theorems need no external success premise. The analysis discards ownership evidence at helper entry, so an outer guard does not justify a read inside a callback or loop body.

Four wider guard forms count as evidence too (recordGuardForms.ts, recordGuardFormsOptional.ts, both required):

  • An enclosing guard: a guard keeps covering its branch after bindings of other names, such as const base = scale * 2. A binding of the record or the key ends it.
  • An early return: on the else side of !Object.hasOwn(r, k), which is the rest of the function after if (!Object.hasOwn(r, k)) return d;, or the rest of a loop body after continue.
  • A bound guard: const has = Object.hasOwn(r, k) makes has a guard, negated or not, until has, r or k is bound again.
  • A reduce callback, with the same per-element proof as map and filter.

The analysis only picks the statement of the theorem. The proof still checks every read, so a wrong guess can only make Lean refuse the certificate. On the negated side, simp states ownership as ¬record.get? key = none. The lemmas getKey_encRecord_of_get and getKey_encRecord_join_of_get read the encoded record from that form. recordGuardOtherKey.ts, recordGuardRebound.ts and recordGuardWrongBranch.ts must still be refused: a guard of another key, a key changed after the guard, and a read in the branch where the key is not own.

The required certificate cases recordGuardedCallback.ts and recordGuardedLoop.ts cover a guarded map and a guarded sum loop over number-valued records. recordGuardedOptionalCallback.ts and recordGuardedOptionalLoop.ts cover an undefined-test callback and a sum loop over optional number values. These helper proofs split the flattened optional payload, not just the outer lookup result, so they cover both an own undefined value and an own number. The corpus reject cases recordCallbackLookup.ts and recordLoopLookup.ts still require refusal for unguarded reads, including reads with a constant ?? fallback: JavaScript may read an inherited Object.prototype property there, so fallback evaluation does not justify replacing that property with undefined. Object.keys and Object.entries remain supported because they enumerate own properties.

Keys with a safe prefix ​

A read needs no guard when its key starts with a literal that starts no name of Object.prototype. Examples are r[`item:${id}`], r["item:" + id] and r[`item:${id}:${n}`]. Such a key can never name an inherited property, so the read finds an own property or undefined. Its function then has no success premise: r[k] ?? d is proved for every key. safeKeyPrefix (Spec/Eval.lean) checks the literal against objectPrototypeNames. The certificate proves that check with decide, then rewrites the lookup with getKey_encRecord_of_prefix or its flattened form. JSString.append_assoc brings the literal to the front of a longer key. The reject cases recordKeyPrototypePrefix.ts (`to${id}`), recordKeyUnderscorePrefix.ts ("__" + id), recordKeyNoPrefix.ts and recordKeySuffixOnly.ts keep these refused. A key bound to a local first, or a literal key alone, still needs a guard. recordSafeKey.ts is the required certificate.

Optional-valued Records use Option.join when printed. This makes a missing property and an own property whose value is undefined both produce the single optional result required by TypeScript.

Literal inherited names, such as toString and constructor, remain refused for dictionary lookups. Object literals with __proto__ also remain refused because that spelling can set the prototype instead of creating an own property.

We decided on 2026-11-30 to recommend Map<string, V> for dictionaries whose keys come from data, and to keep refusing unguarded Record reads. An unguarded read such as counts[key] ?? 0 returns the inherited function Object.prototype.toString when key is "toString". The fallback does not apply, because that value is not undefined. Map.prototype.get (§24.1.3.6) reads only the map's own entries, so m.get(key) ?? 0 has a proof with no premise.

An earlier proposal accepted unguarded reads under a premise that the run never reads a prototype key. We dropped it because that premise depends on the data, so it is weaker than any claim that Tolean makes today. The refusal message now names both fixes: "use a Map, or guard the read with Object.hasOwn(record, key)". The guide section "Dictionaries with string keys" explains the reason with the toString example. The decision on trust annotations later added a third fix, a /** @tolean trusted */ alias, and the message names it too.

Own-key order ​

Object.keys and Object.entries must use the same order as JavaScript: array-index names in ascending numeric order, followed by other names in insertion order. An array-index name is a canonical decimal spelling below 2^32 - 1. Thus 01, -0 and 4294967295 are not array-index names. The evaluator and the printed model share the ordering implementation.

The relevant operations are OrdinaryGet (ECMA-262 2024 §10.1.8.1), OrdinaryOwnPropertyKeys (§10.1.11.1), and Object.hasOwn (§20.1.2.13). The last operation requires ES2022.

Implementation checkpoint ​

The supported certified subset is intentionally narrower than general Record<string, V> indexing: guarded reads in the forms above are accepted. Unguarded reads remain refused. The generic type and own-key enumeration model remain available. An enclosing guard is not evidence for a read inside a callback or loop body: the helper proof for that body does not see the guard.

This page is docs/decisions/2026-11-16-string-record-keys.md at commit 5f1d031.