Skip to content

obj["name"] of a class with a string indexer calls its get/set - #799

Merged
ASDAlexander77 merged 1 commit into
mainfrom
class-string-indexer
Nov 9, 2026
Merged

ASDAlexander77 merged 1 commit into
mainfrom
class-string-indexer

Conversation

@ASDAlexander77

Copy link
Copy Markdown
Owner

Summary

A class that declares [key: string]: T with get/set could be indexed only with a key that isn't a constant. bag["name"], and const k = "name"; bag[k], were read as the member name and failed with "can't resolve property/field/base 'name'" (the // TODO: implement '[string]' access here in mlirGenElementAccess).

A constant key now goes to the indexer when it names no member of the class or of a base class. A key that names a field, method, accessor or static member is still that member, as in TypeScript.

This lets the default library's JsonValue (get/set by key) take value["name"] and value["name"] = member. The DefaultLib change follows once this is merged.

Tests

  • 00class_indexer_string_key.ts: literal, const and let keys, writes, and member names that win (one from a base class). Runs under gc, rc, none and own.
  • export_class_string_indexer.ts / import_class_string_indexer.ts: the same across a shared library, under gc, rc and own.
  • Full Release suite locally: 6854 of 6854 passed.

Not in this PR

Under -mm=own, two writes by key to the default library's JsonValue (person[a] = …; person[b] = …, set declared @lends("this") @keeps("value")) crash tslang.exe when building the exe. It happens on origin/main without this change, also with let keys, and not with direct .set() calls or with a test class of the same shape, in one file or across a shared library. It needs its own fix.

🤖 Generated with Claude Code

A class that declares `[key: string]: T` with get/set could be indexed only
with a key that isn't a constant: `bag["name"]`, and `const k = "name";
bag[k]`, were read as the member `name` and failed with "can't resolve
property/field/base 'name'". A constant key now goes to the indexer when it
names no member of the class or a base class; a key that names a field,
method, accessor or static member is still that member, as in TypeScript.

This lets the default library's JsonValue (get/set by key) take
`value["name"]` and `value["name"] = member`.

Tests: 00class_indexer_string_key.ts (literal, const and let keys, writes,
member names that win, one from a base class) under gc, rc, none and own,
and export_/import_class_string_indexer.ts across a shared library under gc,
rc and own.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ASDAlexander77
ASDAlexander77 merged commit 040f2df into main Nov 9, 2026
8 checks passed
@ASDAlexander77
ASDAlexander77 deleted the class-string-indexer branch November 9, 2026 19:24
ASDAlexander77 added a commit that referenced this pull request Nov 9, 2026
…et (#800)

#799 sent a constant string key to a class's indexer only when the indexer's
key was `string`. With `[key: number | string]: T`, `table[0]` and variable
keys reached get/set, but `table["name"]` was still read as the member `name`
and failed with "can't resolve property/field/base 'name'". A union key that
takes a string now counts as well. A key that names a member is still that
member.

This lets the default library's JsonValue take `value["name"]` and `value[0]`
through one indexer.

Test: 00class_indexer_union_key.ts (string and number keys reaching the same
items, constant and variable keys of both kinds, a member name that wins)
under gc, rc, none and own.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
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