Skip to content

Trust annotations ​

Status: accepted · Date: 2026-11-30

A plain Record<string, V> inherits the properties of Object.prototype, so Tolean refuses an unguarded read such as counts[key] ?? 0 (see the decision on string-keyed records). Much real code reads such records without a guard. Its keys come from data that can never name an Object.prototype member, or it builds the record with Object.create(null). Only the author knows which is true. This record lets the author state it once, on the type, and makes every certificate that depends on it say so.

The decision ​

  • /** @tolean trusted */, or a line @tolean trusted in the JSDoc comment right before a type alias, marks the alias as trusted. For an alias Counts, the translator prints note: assumes trusted Counts (own keys only). The generated Lean file has the same line as a header comment. Every link theorem whose function takes or returns a trusted value says "This link assumes trusted Counts (own keys only)".

  • Each kind of type that can be trusted has exactly one meaning, listed here. A kind that this list does not name cannot be trusted.

    KindMeaning of trusted
    Record<string, V>, Readonly<Record<string, V>>own keys only: every read finds an own property or nothing, never an inherited one
  • The spec (tolean/TypeScript/Spec/Eval.lean) models a trusted Record as Val.dict, an object with no prototype. A read (getOwnKey) looks only at its own properties, which is what OrdinaryGet (ES2024 §10.1.8.1) does when [[GetPrototypeOf]] gives null. The certificate codec encTrustedRecord encodes the Lean JSRecord as such a value. So program_sound and the certificates cover trusted reads with no new premise. The assumption is in the type of the argument: the theorem is about calls whose trusted arguments have no prototype.

  • With that model, r[key], r?.[key], r[key] ?? d, r[key] !== undefined, Object.hasOwn, Object.keys and Object.entries on a trusted Record are accepted without a guard, in function bodies, loops and callbacks.

  • The IR keeps the kind in the type: a trusted Record has its own reference (IR.trustedDictionaryRef). A trusted and a plain Record of the same values are different types, so a plain Record cannot pass where a trusted one is expected. An object literal cannot build a trusted Record, because an object literal has Object.prototype.

  • The feature trustedRecord names the ECMAScript operations that the model follows. ES2015 has them, so every target supports it.

When the assumption is true ​

  • The record was made with Object.create(null) and filled with its own properties.
  • The keys can never be the name of an Object.prototype member, such as user IDs or SKUs from a fixed format. Then a plain object reads the same as one with no prototype.

Parsing a value with Zod or TypeBox does not make it true. A parser checks the own properties, and the result still inherits from Object.prototype.

Refused ​

  • An annotation other than @tolean trusted.
  • Comments come from the lexer: the text between two tokens is the only place a comment can be, so a quote or /* in a string or a regular expression literal never starts one. The JSDoc comment of an alias is the last comment before it, with at most export between them. That comment alone decides whether the alias is trusted. The check for a misplaced @tolean runs over the other block comments and can only refuse.
  • @tolean trusted on an alias of any other type, on an inline type, on a function, or anywhere in a block comment that is not right before a type alias.
  • A Record that is not a named alias. An inline Record<string, V> was already refused; a trusted one needs the alias to carry the annotation.

The refusal of an unguarded read on a plain Record now names all three fixes: a Map, an Object.hasOwn guard, or a trusted alias.

This page is docs/decisions/2026-11-30-trust-annotations.md at commit 5f1d031.