New isolatedDeclarations error with generic this parameter inference Steps to reproduce…
- User
New isolatedDeclarations error with generic
thisparameter inferenceSteps to reproduce
export function fromProviders<T>() { return { tearDown(this: {state: T}): void {} }; }Behavior with
typescript@6.0no error, .d.ts emitted is:
export declare function fromProviders<T>(): { tearDown(this: { state: T; }): void; };Behavior with
tsgoerror TS9008: Method must have an explicit return type annotation with --isolatedDeclarations.Interface
No new interfaces are introduced.
- Assistant
Let me first reproduce the issue:
- Assistant
Reproduced. Now let's find where this diagnostic originates:
- Assistant
Now let me look at how the checker's
Signaturestores thethisparameter to compare against: - Assistant
Now implement the fix in
pseudotypenodebuilder.go. First the method case: - Assistant
Now the single-call-signature case:
- Assistant
Now add the helpers:
- Assistant
No error and correct emit. Let me test edge cases:
- Assistant
Output is correct with no errors. Accept the baselines:
- Assistant
Now run the broader test suite to check for regressions:
- Assistant
While that runs, let me also run the checker package tests:
- Assistant
The full suite passed suspiciously fast — let me verify whether the submodule tests actually ran or were skipped:
- Assistant
While that runs, review the final diff:
- Assistant
All 12,797 submodule test cases pass with no baseline changes. Let me clean up leftover local baselines and run one final full verification:
- Assistant
The fix is complete and verified.
Root cause
With
--isolatedDeclarations, tsgo validates syntactically-inferrable types against the checker's types inpseudoTypeEquivalentToType(internal/checker/pseudotypenodebuilder.go). The pseudochecker'scloneParametersincludes an explicitthisparameter in its parameter list (it's a regular parameter node in the AST), but the checker'sSignature.parametersexcludes it — the checker stores it separately asSignature.thisParameter. The resulting parameter-count mismatch (0 != 1fortearDown(this: {state: T}): void) triggered a spuriousReportInferenceFallback→ TS9008, even though the emitted.d.ts(produced by the checker fallback path) was correct.Changes
internal/checker/pseudotypenodebuilder.go:- Added
splitThisPseudoParameter, which splits an explicit leadingthisparameter off a pseudo parameter list. - Added
pseudoThisParameterMatchesSignature, which validates thethisparameter's type against the checker signature'sthisParameter. - Applied both in the two affected comparison sites: the
PseudoObjectElementKindMethodcase (object-literal methods) and thePseudoTypeKindSingleCallSignaturecase (function expressions), so positional parameters are compared without thethisparameter, which is now checked separately.
testdata/tests/cases/compiler/isolatedDeclarationsThisParameter.ts(+ accepted reference baselines): covers the original repro (genericTin thethistype),thisplus regular/optional parameters,this: void, and a function expression with athisparameter. All emit correct declarations with no errors, reusing the source annotations (the pseudo path, not the checker fallback).Verification
- Original repro now compiles cleanly and emits the same
.d.tsas strada. - Full test suite passes: all 12,797 submodule compiler tests + local tests, with no baseline changes beyond the new test.
go build/go vetclean.
One pre-existing, unrelated behavior noted while testing: returning a named
as constlocal still errors with TS9039 (reproduced on the base commit as well), consistent with the type needing to be nameable. - Added
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Identical first prompt
- composer-2.5Compare side by side →
- composer-2.5Compare side by side →
- composer-2.5Compare side by side →
- composer-2.5Compare side by side →
- composer-2.5Compare side by side →
- gpt-5.6-solCompare side by side →
- gpt-5.6-solCompare side by side →
- gpt-5.6-solCompare side by side →
- gpt-5.6-solCompare side by side →
- gpt-5.6-solCompare side by side →
- fable-5Compare side by side →
- fable-5Compare side by side →