ISSUE DESCRIPTION New isolatedDeclarations error with generic this parameter inference…
- User
ISSUE DESCRIPTION
New isolatedDeclarations error with generic
thisparameter inferenceSteps to reproduce
typescriptexport function fromProviders<T>() { return { tearDown(this: {state: T}): void {} }; }Behavior with
typescript@6.0no error, .d.ts emitted is:
typescriptexport declare function fromProviders<T>(): { tearDown(this: { state: T; }): void; };Behavior with
tsgocodeerror TS9008: Method must have an explicit return type annotation with --isolatedDeclarations.Interface
No new interfaces are introduced.
ADDITIONAL ADVICE
Since you are given a git repository, you can use git commands to simplify your work. For example, if you made a mistake and want to revert your changes, you can use
git checkout <file>to restore the file to its original state. You can also reset all changes in the repository usinggit reset --hardcommand. Additionally, you can usegit stashandgit stash popto temporarily save your changes and restore them later. Finally, you can see the changes that you've made (which will be submitted when you callsubmit) usinggit statusorgit diffcommands. However you don't need to usegit addorgit commitbefore submitting your changes. In fact,submitonly submits currently unstaged changes because it usesgit diffto get the changes that need to be submitted. Assistant
Let me explore the repository structure to understand what we're working with.
Assistant
This is the TypeScript native port (tsgo). The issue is about
isolatedDeclarations- a method with athisparameter should not require an explicit return type annotation. Let me look into the relevant code.Assistant
Let me look at the whole file to understand the logic. Let me find the function that determines whether a function needs an explicit return type.
Assistant
Now let me look at where
createReturnTypeErroris and thecreateGetIsolatedDeclarationErrorsis called from. Let me understand the logic that determines whether a method needs an explicit return type.Assistant
Let me reproduce the issue first.
Assistant
Let me build tsgo and run it on the repro file.
Assistant
Reproduced. Now let me understand the logic. Let me find where the node builder triggers this error for methods with a
thisparameter.Assistant
Let me look at the main node builder to understand how function-like declarations are serialized.
Assistant
Now let me look at
tryGetThisParameterDeclarationandsymbolToParameterDeclaration, andserializeReturnTypeForSignature. Let me look atserializeReturnTypeForSignaturefirst.Assistant
Now let me look at
symbolToParameterDeclaration.Assistant
Let me look at
pseudoTypeEquivalentToTypemore closely, the parameter comparison part.Assistant
The likely culprit: the pseudochecker's
pt.Parametersmight include thethisparameter whiletargetSig.parametersdoes not (it's stored inthisParameter). Let me check the pseudochecker.Assistant
Let me look at where
cloneParametersis called (line 530).Assistant
Let me confirm how the checker handles the
thisparameter insignature.parameters.Assistant
Let me look at line 19698-19763, where the signature is built from the declaration, to confirm
thisis excluded fromparameters.Assistant
Confirmed: the checker excludes the
thisparameter fromsignature.parameters, but the pseudochecker'scloneParametersincludes it. Let me test with a non-genericthisparameter to confirm the scope.Assistant
Confirmed it's a general bug with
thisparameters, not specific to generics. Let me also test a few more cases to understand the scope.Assistant
Let me look at the
PseudoTypeKindObjectLiteralcase inpseudoTypeEquivalentToType.Assistant
Let me look at how
pseudoTypeToNodehandles theSingleCallSignatureand object method cases (lines 157-290).Assistant
Let me look at
pseudoParameterToNodefully.Assistant
Let me look at how a
thisparameter is represented in the AST.Assistant
Let me check how the parser handles
thisparameters to understand the name node type.Assistant
Let me look at where
pseudoTypeEquivalentToTypeis called and how the result is used (for object literals and signatures).Assistant
Let me look at
serializeTypeForDeclaration(line 2116 onwards).Assistant
Let me look at
getSingleCallSignature.Assistant
Let me look at
getTypeOfParameterand related helpers.Assistant
Let me look at the existing testdata for isolated declarations to understand the test format and find related tests.
Assistant
Let me look at one of these test files to understand the test format.
Assistant
Let me re-read block 1 (PseudoObjectElementKindMethod) fully.
Assistant
Let me write a small test to print the AST of the repro to confirm the
thisparameter's name node kind.Assistant
Let me not bother with the standalone test; instead, let me add a debug print in the comparison code and run the repro. Actually, let me just look at the parser
createIdentifierto confirm.Assistant
Let me test whether a non-
thisparameter with a type parameter type works (to understand the scope handling for type params).Assistant
No error for a non-
thisparameter with typeT. Let me see the emitted output.Assistant
Now I understand the issue fully. Let me implement the fix. First, let me add a helper to detect a
thispseudo-parameter. Let me view the area aroundpseudoParametersToNodeListto add the helper.Assistant
astis imported. Now let me add the helper function afterpseudoParametersToNodeList.Assistant
Now let me add the helper function. I'll insert it after
pseudoParameterToNode(line 326).Assistant
Let me add a blank line for cleanliness.
Assistant
Now let me modify block 1 (PseudoObjectElementKindMethod). Let me view the current lines.
Assistant
Now let me modify block 1 (PseudoObjectElementKindMethod) to handle the
thisparameter.Assistant
The indentation is off (should be 4 tabs inside the case). Let me fix with gofmt.
Assistant
Block 1 looks good. Now let me modify block 2 (PseudoTypeKindSingleCallSignature). Let me view it.
Assistant
Now let me modify block 2 (lines 551-572).
Assistant
Now let me add the two helper functions. Let me add them near
isPseudoThisParameter.Assistant
Let me build and test the repro cases.
Assistant
Both cases now produce the expected output with no error. Let me test the other cases (repro3) and more edge cases.
Assistant
All cases produce expected output. Let me test more edge cases.
Assistant
Now let me test cases that SHOULD still produce errors (to make sure I didn't over-suppress).
Assistant
Let me test a case where the
thisparam type references an inaccessible (non-exported) name - this should error.Assistant
Let me test a case where the return type genuinely cannot be inferred (should still error).
Assistant
The legitimate error is still reported. Now let me run the existing test suite to make sure I didn't break anything. Let me find the test infrastructure.
Assistant
Let me find how the compiler test cases are run (the testdata harness).
Assistant
Let me check the existing baselines for the isolatedDeclarations test cases and how to run a specific test.
Assistant
The submodule tests require the TypeScript submodule which is empty. The local tests (TestTypeRegression / "compiler") use
testdata/tests/cases/compiler. Let me check whether there are baselines for the local compiler tests. The local tests have baselines intestdata/baselines/reference/local/perhaps. Let me check.Actually, the existing isolatedDeclarations test files are in
testdata/tests/cases/compiler/(TestTypeRegression). Let me check their baselines location.Assistant
Let me run the local compiler tests. First, let me check if there's a way to filter to a specific test.
Assistant
Hmm, it ran quickly and said ok, but I'm not sure it actually ran the isolatedDeclarations tests (the
-runfilter might not match the subtests). Let me check with verbose to confirm tests ran.Actually wait,
okwith 0.097s means it ran but maybe the filter matched the top-levelTestLocaland ran nothing? No,TestLocalitself is the function name. The-run 'TestLocal/isolatedDeclarations'filters subtests under TestLocal. Let me run with -v.Assistant
The isolatedDeclarations tests pass. Per the instructions, I won't add new test files. Let me run the broader test suites to ensure nothing breaks. Let me run checker and declarations-related tests.
Assistant
Let me run the full local compiler test suite (TestLocal) which validates declaration emit baselines.
Assistant
That ran fast. Let me check how many local compiler tests exist and verify the count.
Assistant
All local compiler tests pass (no failures). Let me also run the fourslash code-fix tests for missing type annotations, since they touch the ID error path.
Assistant
The fourslash tests are individual
Test*functions. Let me run the codeFixMissingTypeAnnotationOnExports ones (generated).Assistant
Let me verify these tests actually run (verbose, count).
Assistant
Let me check the generated test names and whether there's a realistic
this-without-annotation edge case. Let me testthiswithout a type annotation.Assistant
thiswithout annotation (function declaration) is already handled with TS9011. Let me test the object-literal method case with untypedthis.Assistant
This untyped-
this-in-object-method is a bizarre edge case. Let me compare with the pre-change behavior using git stash.Assistant
The binary wasn't rebuilt after stash, so that wasn't a valid comparison. Let me properly rebuild with the old code and compare.
Assistant
OLD and NEW behavior for the untyped-
thisedge case are identical - no regression. Let me verify the already-working cases (repro4) still produce identical output (no regression).Assistant
The diff comparison is confusing due to interleaved output. Let me compare error output and .d.ts separately and cleanly for repro5.
Assistant
The fix removes the 3 false-positive errors while keeping the .d.ts output identical. Let me run targeted fourslash tests related to declarations/isolated/missingType to be safe.
Assistant
Let me verify these fourslash tests actually run (not all skipped) with verbose on a couple.
Assistant
Hmm, "no tests to run". The test name is
TestCodeFixMissingTypeAnnotationOnExportsArrowParens. Why doesn't-run 'TestCodeFixMissingTypeAnnotationOnExportsArrowParens$'match? Maybe the build tags. Let me check the file header.Assistant
The function name has an underscore:
TestCodeFixMissingTypeAnnotationOnExports_arrowParens. Let me run it correctly.Assistant
These fourslash tests are skipped (no TypeScript submodule). Let me check for
internal/lstests related to code actions that reference ID diagnostics.Assistant
All tests pass. Let me review my final diff.
Assistant
The diff looks clean. Let me test a few more
thistype variations (generic type references) to be thorough.Assistant
All variations work. Let me run
go vetandgofmt -lon the modified file to ensure code quality.Assistant
gofmt clean and go vet passes. Let me run the full local compiler test suite one final time to confirm no regressions.
Assistant
All tests pass (0 FAIL lines; the exit code 1 is just
grep -creturning 0 matches). Let me verify the repo only has my intended change.Assistant
Only the intended file is modified. Let me do a final verification of the exact issue repro.
Assistant
The issue is fully resolved. The error is gone and the
.d.tsmatches TypeScript 6.0's expected output exactly.Let me go through the checklist with my reasoning:
- ✅ Reproduced the error: Before the fix,
tsgoemittederror TS9008: Method must have an explicit return type annotation with --isolatedDeclarations.for the repro (and confirmed it's a general bug with anythisparameter in object-literal methods/function expressions, not just generics). - ✅ Confirmed the fix resolves the error: After the fix, no error is produced and the
.d.tsoutput matches TS 6.0 exactly (tearDown(this: { state: T; }): void;). - ✅ Considered edge cases: Tested generic/non-generic , with additional regular params, function expressions (the path), function declarations, object-literal methods, with generic type references (e.g. ), untyped (verified identical to pre-change behavior — no regression), and confirmed legitimate errors (un-inferrable return types) are still reported. The root cause: the pseudochecker includes the leading parameter in its parameter lists while the checker stores it separately in and excludes it from , causing a spurious count mismatch. The fix splits the parameter off and compares it against instead.
- ✅ Reproduced the error: Before the fix,
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.