Port TypeScript PR#60980: Fix wrong error on required initialized parameters with isolatedDeclarations #1857
+1
−1
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Ports microsoft/TypeScript#60980 which fixes an issue where
requiresAddingImplicitUndefined
received the wrong enclosing declaration parameter when checking constructor parameter properties with initializers.Problem
When processing constructor parameter properties with initializers under
isolatedDeclarations
, the wrong enclosing declaration was passed toRequiresAddingImplicitUndefined
. Consider:The parameter property
arr
has both a type annotation and an initializer. TheisRequiredInitializedParameter
function should detect this and return true, but it was receiving the class as the enclosing declaration instead of the constructor.Root Cause
In
ensureType
, when checking parameters:For constructor parameters,
tx.enclosingDeclaration
is the class. However,isRequiredInitializedParameter
checks:Since the class is not a function-like declaration, this returns
false
incorrectly.Solution
Changed line 1019 in
internal/transformers/declarations/transform.go
to passnode.Parent
(the constructor) instead oftx.enclosingDeclaration
(the class):Now the constructor is correctly identified as a function-like declaration, and the function returns
true
as expected.Impact
This fix prepares the codebase for when isolated declarations error reporting is fully implemented. The underlying logic now correctly identifies parameter properties with initializers as requiring implicit undefined, aligning the Go port with the TypeScript implementation.
Note: The full isolated declarations diagnostic reporting feature is not yet fully ported (marked with TODO in
diagnostics.go
), so there are no immediate visible changes or baseline updates. However, this fix is essential for correct behavior when that feature is completed.Fixes microsoft/TypeScript#60976
Original prompt
Note
Custom agent used: Strada to Corsa Port Expert
A Go and TypeScript expert who can easily figure out how to port PRs from one language to another
💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.