Skip to content

fix(resolution): a TS local set by a member call on an imported const takes the type the member returns - #3616

Open
inth3shadows wants to merge 1 commit into
colbymchenry:mainfrom
inth3shadows:fix/3572-imported-const-method-result
Open

inth3shadows wants to merge 1 commit into
colbymchenry:mainfrom
inth3shadows:fix/3572-imported-const-method-result

Conversation

@inth3shadows

@inth3shadows inth3shadows commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #3572

What

const parent = client.create({}); parent.extend({}), where client is a const imported from another file (export const client: Client = makeClient()), now links parent.extend to Client::extend, as it already did since #3422 when client is a parameter or a const of the same file. Before, parent got no type, so the call linked only when extend was the one method of that name in the project.

How

tsValueType (name-matcher.ts#L28203) read the receiver only from a binding in the call's own file. When there is none and the file imports the name, it now follows that import to the project's top-level const and reads what its declaration says, in the declaring file: the type written on it, with the names in that type resolved there. Then it carries on as for a same-file const. This covers a capitalized const too (API.create()), which before stopped at the check for a class of that name.

  • Only an imported const is read. A let or var the module may reassign, a const with nothing written that says its value, and an import from a package are read as before.
  • The const's file is part of what the answer stands on, so a sync that changes the const's type re-reads the call.
  • What an import resolves to, and what a const's declaration says, are each read once per index pass, not once per call.

Designs tried and dropped during review on this branch:

  • Making a call that links nothing wait on the member's file. An edit to the const's type then left the call stale. It now waits on the const's file (see Known limits for the other direction).

Measured (base 8ef378de66e65a7d4986d53ae7b8d86cfc4118af vs this branch, Node 22.23.2, Linux x64, CODEGRAPH_KERNEL=0)

  • New tests: 6 in ts-member-call-result-types.test.ts, which passes 11/11. On main the 5 bug tests fail and the other 6 pass, among them the new case that an imported const with no type and a typed let are read as before.
  • Related suites: 16 files of receiver, import-binding, sync and large-file lookup tests, 190/190.
  • Real repos: five TypeScript projects of mine (about 1,090,000 lines of graph dump in all, compare-graphs.mjs against 73cd6e48, with the kernel and with CODEGRAPH_KERNEL=0): 0 changed lines.
  • Cost, one project with many imported consts (100 imports per file, each const called once):
    • 3,000 consts in one 108 KB file: main 5.8 s / 5.6 s, this branch 6.9 s / 6.6 s.
    • 12,000 consts in one 430 KB file: main 15.6 s, this branch 21.5 s.
    • The extra time grows about as the input does, and the branch links one more call per use (p.extend()) that main leaves to the name match.
    • 24 consts in 24 files of 280 KB each, 1,440 calls: main 11.9 s / 11.5 s, this branch 10.6 s / 10.1 s.
  • Regression gate (npm run gate, native kernel built, on the rebased tree): PASS: 174 projects, 171 growth cases.
  • Full suite on the final tree (rebased on 73cd6e48847850ede14554b9b84e04e9f2d4dc2f): 13,689 passed, 0 failed, 2 expected fail. Platforms run: Linux x64; not tested on: macOS, Windows.
  • Compatibility: no migration; a re-index picks the fix up.
  • The cost numbers were taken on 8ef378de66e65a7d4986d53ae7b8d86cfc4118af, before the rebase onto 73cd6e48847850ede14554b9b84e04e9f2d4dc2f (fix(rust): indexing no longer hangs on uses that start with their own name #3611), where only the changelog conflicted. The suite, gate and real-repo diff were run again on the rebased tree. The last change before measuring (reading each import once per file) was checked by the gate and the full suite, not by a further review round.

Checked with graph-assay

I maintain graph-assay. Its fixture issue-3572, the issue's own files, goes from BROKEN on main (73cd6e48847850ede14554b9b84e04e9f2d4dc2f) to FIXED on this branch. No other fixture verdict changed. Side effects: none on the measured repos.

Rerun: node bin/graph-assay replay <this branch>/dist/bin/codegraph.js issue-3572

Known limits

  • A call that links nothing waits for one file to change before a sync tries it again. Through an imported const that is the const's file, so an edit made only to the member's type in another file (get: () => Response becoming get: () => Body) takes effect when the call's own file is indexed again. A local receiver on main has the same one-file wait.
  • A linked call goes stale after a sync when only a type further down changes, such as an alias the member's return type names (type R = Local becoming Local2). main does the same for a local receiver.
  • A const whose type could not be read (no type written, or a type from a package) that later gets a project type is not re-read on sync; main does the same for the first call on it.
  • A namespace import (ns.client.create()) is read as before.

Edit (2026-12-03): the suite, the gate and the real-repo diff were run again on the rebased tree; the Measured numbers above are from that run.

Edit (2026-12-03): added the graph-assay section, after its fixture for this issue merged.

inth3shadows added a commit to inth3shadows/codegraph that referenced this pull request Dec 3, 2026
inth3shadows added a commit to inth3shadows/codegraph that referenced this pull request Dec 3, 2026
inth3shadows added a commit to inth3shadows/codegraph that referenced this pull request Dec 4, 2026
… takes the type the member returns (colbymchenry#3572)

const parent = client.create({}); parent.extend({}) linked parent.extend to
Client::extend only when client was a parameter or a const of the same file
(colbymchenry#3422). A const the file imports from a project module is now read where it
is declared: the type written on it, with its names resolved in that file,
including a capitalized const (API.create()). A let or var, a const with
nothing written that says its value, and an import from a package are read as
before. The const's file is recorded for a sync, and what an import resolves
to and what a const's declaration says are each read once per pass.
@inth3shadows
inth3shadows force-pushed the fix/3572-imported-const-method-result branch from 9d4e852 to d9242f3 Compare December 4, 2026 23:17

This branch has not been deployed

No deployments
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.

TypeScript: a local set by a method call on an imported const gets no type (follow-up to #3422)

1 participant