Skip to content

Keys known at run time: a string index signature is a dictionary (TSLANG 152) - #972

Merged
ASDAlexander77 merged 9 commits into
mainfrom
runtime-keys-dict
Nov 21, 2026
Merged

ASDAlexander77 merged 9 commits into
mainfrom
runtime-keys-dict

Conversation

@ASDAlexander77

Copy link
Copy Markdown
Owner

This implements your decisions: a string index signature is a dictionary, reading an absent key gives T | undefined, and under -mm=own the dictionary owns its values. The library side is ASDAlexander77/TypeScriptCompilerDefaultLib#114 (merged): it adds ObjectDictionary<T>, marked @dictionary, and objectDictionaryAssign.

What changed

  • Dictionary types: { [key: string]: T }, a type literal with a string index signature and nothing else, is now ObjectDictionary<T>.
    • Before, it was a record of get/set fields that nothing could fill, so const d: { [k: string]: number } = { a: 1 } failed.
  • Entries, not members: outside the class, d[k], d["name"] and d.name always mean entries, so d.get and d.size are entries too.
    • A read gives T | undefined; a write takes T.
    • k in d, for...in, Object.keys(d) and delete d[k] / delete d.name all go by the entries.
  • Object literals that make dictionaries:
    • A literal with a key known only at run time ({ [m]: i }) makes a dictionary of its values' type.
    • So does a literal given where a dictionary is expected: an annotated variable, an argument or a return.
    • Spreads copy a record's fields, or a dictionary's entries through objectDictionaryAssign.
    • A key still counts as a constant in the same cases as before: an imported const, or a folded "a" + "b".
  • -mm=own: a parameter moved on paths that exclude each other is no longer reported as "used after its value was moved". This covers if/else, a move then return then another move, and a narrowed T | undefined. The dictionary's set needs this.
  • Fixed along the way: a module that imports another and uses a Map failed with "'ts.Func' op symbol declaration cannot have public visibility". The keys adapter for what the library boxes into any was made as a public declaration while the imported file was being read. This bug was already in main.

Verification

  • Full ctest: 8761 tests, 0 failures.
  • New tests, each run in JIT under gc, rc and own: 00object_dictionary.ts, 00own_param_exclusive_moves.ts and import-map. The tester compiles without the default library, so these tests declare their own dictionary class.
  • The import-map test does not reproduce the Map import bug: that path needs the default library, which the tester doesn't load. It covers a dictionary keyed by imported values.
  • TypeScript compiler-tests sweep: 3238 → 3244 passing, with no crashes.
    • The only test that went from passing to failing is numericIndexerConstraint1. TypeScript reports that line as an error too, and tslang now does as well.
  • Leak probes with PrivateMemorySize, memory stayed flat at 33 MB:
    • 20M calls moving a parameter on exclusive paths (-mm=own).
    • 3M writes, overwrites and deletes on a dictionary of objects (rc and own).

Still open, recorded in TSLANG 152:

  • An interface with only a string index signature is not mapped to a dictionary.
  • Under own, spreading a record into a dictionary is refused.
  • Under own, a local moved inside two ifs in a row is still refused.
  • Existing generic-inference bug: inferring f<T, S>(Box<T>, Box<S>) gives the wrong types and then crashes. Because of it, a spread between dictionaries with different value types is refused.
  • Existing bug: a[a.length] = x does not grow an array.

🤖 Generated with Claude Code

ASDAlexander77 and others added 9 commits November 21, 2026 10:41
… a move then return, a narrowed T | undefined) is no longer a use after move

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… default library's ObjectDictionary<T>; d[k], d.name, k in d, for...in, Object.keys and delete go by its entries; { [m]: v } with a run-time key makes one

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…oved on exclusive paths

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…onst's accessor was declared twice); detected from the generated key

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…eclaration it was public, which failed every module importing another that made one (import + new Map<...>)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ber is made by the check; an import-map test

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…les without the default library

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ASDAlexander77
ASDAlexander77 merged commit c30eb26 into main Nov 21, 2026
8 checks passed
@ASDAlexander77
ASDAlexander77 deleted the runtime-keys-dict branch November 21, 2026 12:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant