Skip to content

feat(ts): type-check methods on generic types - #20

Merged
kixelated merged 6 commits into
mainfrom
quest/m2/ts-generic-methods
Oct 24, 2026
Merged

kixelated merged 6 commits into
mainfrom
quest/m2/ts-generic-methods

Conversation

@kixelated

@kixelated kixelated commented Oct 24, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

Each monomorphized method of a generic struct lands on the one generic class, so its body sees this.value: T and calls an instance's method on it (this.value.fmt$Pair(f)). tsc rejects every such method, derived Debug included. moq-net's model has generic types, so the model quest needs this. Finishes quest/m2/ts-generic-methods.md.

Approach

  • A generic class's instance method declares the instance it belongs to as a TS this parameter: fmt$Pair(this: Wrap<Pair>, f: rt.Formatter). Its body then sees this.value: Pair, and a call on the wrong instance is a tsc error. Non-generic classes and companion functions (which already take a concrete self) print as before.
  • A class constructor keeps a literal's as where the field's type is a class parameter, since TS infers the parameter from it and would widen { tag: "Right", value: 500 } to { tag: string; ... }.
  • Instance suffixes name a generic type's arguments (get$Wrap_Pair, fmt$Option_u8, fmt$Vec_u16), skipping defaults such as Vec's allocator, and a default method on a generic class is suffixed by the class's arguments rather than the class (describe$Meters). Before, both collided into numbered names such as describe$Wrap$2.
  • New lang fixture module generics.rs, run differentially against native Rust: Wrap<T> with methods on every instance, on T: Area, and on Wrap<u8> only, a method with its own type parameter, a trait impl and default method, derived Debug/PartialEq (compact and pretty) over structs, enums, numbers, Option, Vec, Result, and Wrap<Wrap<Pair>>; a generic enum Either<L, R> with companion functions and flip to Either<R, L>; and Tagged<K, V> nesting Wrap<V>.

Impact

  • Translator output: instance methods of generic classes gain a leading this: C<...> parameter. JS callers are unaffected; TS callers get a type error when they call another instance's method.
  • Translator output: suffixes of instances over generic types now include the type's arguments (fmt$Wrap_Pair where it was fmt$Wrap, or a numbered $2 on a collision). No existing fixture or moq name changes.
  • Census: moq-net's census counts 32 more refusals (15563 from 15531), each the self of a method on a generic type instantiated with a type outside the subset (Arc, kio::Producer, Bytes), since the method's signature now names that type. kio's census is unchanged.

Alternatives

  • A class per instance (class Wrap$Pair): exact too, but renames the type TS callers name, duplicates each class per instance, and breaks instanceof across instances. Larger change for the same result.
  • TS generics with trait dictionaries (fmt(this: Wrap<T>, dT: Debug<T>, f)): one method per Rust method, but it undoes monomorphization, adds a dictionary parameter to every call, and needs a TS encoding of every trait the subset uses.
  • Explicit type arguments on new (new Tagged<Either<number, number>, number>(...)) instead of keeping as: also exact, but adds noise to every generic constructor where the kept as only appears on literals that need it.

Follow-ups

  • Option's PartialEq and Option::or have no mapping in library.rs; the fixture avoids them. The census already ranks unmapped std calls.

Checks: just check and just test pass; just ts moq translates and type-checks; just ts census runs (diff above). Before the fix, the new fixture failed tsc on every instance method of a generic class.

(written by Opus 5.5)

🤖 Generated with Claude Code

https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn

kixelated and others added 3 commits October 24, 2026 00:07
@kixelated kixelated changed the title Methods on generic types in the TypeScript translator feat(ts): type-check methods on generic types Oct 24, 2026
@kixelated
kixelated marked this pull request as ready for review October 24, 2026 07:36
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 24, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review 🔄 Running since 2026-10-24T07:56:12.072024Z 8139069 New commits
🔒 Security Review ✅ Completed 2026-10-24T07:58:12.153059Z 8139069 New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: fb5314e933

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread ts/src/lower/mod.rs
Comment on lines +963 to +966
std::iter::once(tcx.item_name(adt.did()).to_string())
.chain(args.iter().filter_map(|arg| arg.as_type()).map(|t| short_name(tcx, t)))
.collect::<Vec<_>>()
.join("_")

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Add a migration entry for changed generated symbol names

For TypeScript consumers that call exported monomorphized functions, including ADT arguments in suffixes changes existing public names, such as get$Wrap to get$Wrap_Pair, and breaks those callers. The README explains the new naming but provides no migration entry. A repository-wide filename search and the reviewed commit's Git tree contain no MIGRATE.md. Add ts/MIGRATE.md with an ## Unreleased entry describing the naming change and explicit this restrictions, as required by AGENTS.md.

AGENTS.md reference: AGENTS.md:L45-L46

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not adding one: rust2-ts is unreleased (version = "0.0.0", publish = false) and has no MIGRATE.md, as with the earlier output changes in #8 and #18. No consumer sees a rename either: moq's translation (just ts moq) and the existing fixtures print identical names, and the this parameter only lands on methods that failed tsc before this PR. The PR's Impact section records both output changes.

(written by Opus 5.5)

…ethods

# Conflicts:
#	quest/m2/README.md
#	quest/m2/ts-model.md
#	ts/README.md
…ethods

# Conflicts:
#	quest/m2/README.md
#	quest/m2/ts-model.md
@kixelated

Copy link
Copy Markdown
Collaborator Author

This makes methods on generic types type-check: each instance method takes its own instance type as a TS this parameter, and instance names now include their type arguments.

Merging.

(written by Opus 5.5)

@kixelated
kixelated merged commit 0445c42 into main Oct 24, 2026
1 check passed
@kixelated
kixelated deleted the quest/m2/ts-generic-methods branch October 27, 2026 20:32
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