Skip to content

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Oct 9, 2025

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 to RequiresAddingImplicitUndefined. Consider:

class Bar {}
export class ClsWithRequiredInitializedParameter {
  constructor(
    private arr: Bar = new Bar(),
    private bool: boolean,
  ) {}
}

The parameter property arr has both a type annotation and an initializer. The isRequiredInitializedParameter 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:

tx.resolver.RequiresAddingImplicitUndefined(node, nil, tx.enclosingDeclaration)

For constructor parameters, tx.enclosingDeclaration is the class. However, isRequiredInitializedParameter checks:

if ast.HasSyntacticModifier(parameter, ast.ModifierFlagsParameterPropertyModifier) {
    return enclosingDeclaration != nil && ast.IsFunctionLikeDeclaration(enclosingDeclaration)
}

Since the class is not a function-like declaration, this returns false incorrectly.

Solution

Changed line 1019 in internal/transformers/declarations/transform.go to pass node.Parent (the constructor) instead of tx.enclosingDeclaration (the class):

tx.resolver.RequiresAddingImplicitUndefined(node, nil, node.Parent)

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

From the TypeScript repo, port PR#60980

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.

@Copilot Copilot AI changed the title [WIP] Port PR#60980 from TypeScript repository Port TypeScript PR#60980: Fix wrong error on required initialized parameters with isolatedDeclarations Oct 9, 2025
@Copilot Copilot AI requested a review from RyanCavanaugh October 9, 2025 21:44
Copilot finished work on behalf of RyanCavanaugh October 9, 2025 21:44
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.

[isolatedDeclarations] --isolatedDeclarations gives incorrect error when a parameter property with default value is followed by required parameter
2 participants