Skip to content

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Sep 25, 2025

Fixes an issue where definite assignment assertions (!) were incorrectly preserved in TypeScript declaration files for class properties, causing compilation errors.

Problem

When generating declaration files (.d.ts), the TypeScript compiler should remove definite assignment assertions since they are not valid in ambient contexts. However, tsgo was preserving these assertions on class properties, leading to:

// Source file
export class DbObject {
    id!: string;
}

// Generated .d.ts (incorrect)
export declare class DbObject {
    id!: string;  // Error: A definite assignment assertion '!' is not permitted in this context
}

Solution

Modified the declaration transformer to detect and remove exclamation tokens (KindExclamationToken) from class property declarations when generating declaration files. The fix only applies to property declarations in classes, as property signatures in interfaces cannot have definite assignment assertions.

Files changed:

  • internal/transformers/declarations/transform.go: Updated transformPropertyDeclaration to filter out definite assignment assertions

Result

The fix now correctly generates declaration files without definite assignment assertions on class properties:

// Generated .d.ts (correct)
export declare class DbObject {
    id: string;  // Clean declaration without assertion
}

This change aligns tsgo's behavior with the official TypeScript compiler and eliminates the compilation errors that were occurring in declaration files.

Testing

  • Verified using existing test coverage in definiteAssignmentAssertions.ts and other submodule tests
  • All existing tests pass with updated baselines showing proper removal of ! tokens from class properties
  • Confirmed property signatures in interfaces remain unchanged (as they should)

Fixes #1736

Original prompt

This section details on the original issue you should resolve

<issue_title>Definitely assign assertion preserved on field in declaration files</issue_title>
<issue_description>## Steps to reproduce

export class DbObject {
    id!: string;
}

Playground Link

Behavior with [email protected]

The ! is removed from declaration files

Behavior with tsgo

The ! is preserved in declaration files

export declare class DbObject {
    id!: string;
}
```</issue_description>

<agent_instructions>Add a test like:

```ts
// @declaration: true
// @strict: true
export class DbObject {
    id!: string;
}

Accept the baselines, note the output and then fix the bug in the declaration transform.</agent_instructions>

Comments on the Issue (you are @copilot in this section)

Fixes #1736

💬 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] Definitely assign assertion preserved on field in declaration files Fix: Remove definite assignment assertions from declaration files Sep 25, 2025
@Copilot Copilot AI requested a review from jakebailey September 25, 2025 19:02
Copilot finished work on behalf of jakebailey September 25, 2025 19:02
Copilot finished work on behalf of jakebailey September 25, 2025 19:47
@Copilot Copilot AI requested a review from jakebailey September 25, 2025 19:47
@Copilot Copilot AI requested a review from jakebailey September 25, 2025 20:01
Copilot finished work on behalf of jakebailey September 25, 2025 20:01
@jakebailey jakebailey marked this pull request as ready for review September 25, 2025 20:01
@Copilot Copilot AI review requested due to automatic review settings September 25, 2025 20:01
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes a bug where definite assignment assertions (!) were being preserved in declaration files when they should be removed. The TypeScript compiler removes these assertions from .d.ts files since they are only relevant during compilation and not in the final type declarations.

  • Added test cases to verify the fix for definite assignment assertions in declaration files
  • Implemented logic to remove exclamation tokens from property declarations in declaration files
  • Updated existing baselines to reflect the corrected behavior

Reviewed Changes

Copilot reviewed 18 out of 18 changed files in this pull request and generated no comments.

File Description
testdata/tests/cases/compiler/definiteAssignmentAssertionInDeclaration.ts Basic test case for definite assignment assertion removal
testdata/tests/cases/compiler/definiteAssignmentAssertionInDeclarationExtended.ts Extended test case covering various property types and modifiers
internal/transformers/declarations/transform.go Implementation of the fix to remove exclamation tokens from declaration files
Multiple baseline files Updated reference outputs showing corrected behavior with exclamation tokens removed

@jakebailey
Copy link
Member

@copilot Remove the newly added tests and baselines, leaving only the changes to the existing test baselines. The existing tests turned out to cover everything.

Copy link
Contributor Author

Copilot AI commented Sep 25, 2025

@copilot Remove the newly added tests and baselines, leaving only the changes to the existing test baselines. The existing tests turned out to cover everything.

Removed the newly added test files and their baselines. The existing tests in definiteAssignmentAssertions.ts and other submodule tests already provide adequate coverage. Fixed in commit cd2565a.

Copilot finished work on behalf of jakebailey September 25, 2025 20:14
@Copilot Copilot AI changed the title Fix: Remove definite assignment assertions from declaration files Fix: Remove definite assignment assertions from class property declaration files Sep 25, 2025
@Copilot Copilot AI requested a review from jakebailey September 25, 2025 20:56
Copilot finished work on behalf of jakebailey September 25, 2025 20:56
@jakebailey jakebailey added this pull request to the merge queue Sep 25, 2025
Merged via the queue into main with commit 00315a5 Sep 25, 2025
42 checks passed
@jakebailey jakebailey deleted the copilot/fix-e75565ee-d32e-4143-97a5-3fa1a505dd1a branch September 25, 2025 23:04
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.

Definitely assign assertion preserved on field in declaration files
4 participants