Skip to content

Fix: No error when assigning to getter-only static class property from another file #1095

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Jun 7, 2025

This PR fixes an issue where TypeScript-Go failed to report errors when assigning to getter-only properties imported from another module.

Problem

// export.ts
export class MyClass {
  static get foo() {
    return 42;
  }
}

// import.ts
import { MyClass } from "./export";
MyClass.foo = 0; // Should be error TS2540, but no error was reported

Expected: Error TS2540: "Cannot assign to 'foo' because it is a read-only property"
Actual: No error reported

Root Cause

Two issues in the assignment checking logic in internal/checker/checker.go:

  1. Missing readonly check for simple assignments: checkAssignmentOperator only called checkPropertyAccessExpression with writeOnly=true for compound assignments (+=, -=, etc.), but not for simple assignments (=)

  2. Early return for named imports: isAssignmentToReadonlyEntity returned early for named imports instead of checking if the target symbol was readonly

Solution

Minimal 7-line fix with two changes:

  1. Added readonly check for simple assignments: Now calls checkPropertyAccessExpression(writeOnly=true) for simple assignments to property access expressions, matching the behavior already implemented for compound assignments

  2. Fixed alias handling: Modified isAssignmentToReadonlyEntity to only return early for namespace imports, allowing readonly checks to proceed for named imports

Testing

✅ Cross-file assignments to getter-only properties now correctly report TS2540
✅ Same-file behavior unchanged
✅ Properties with setters work correctly (no error)
✅ Both instance and static properties work
✅ All existing checker tests pass

The fix correctly handles various scenarios:

  • Static getter-only properties (main issue)
  • Instance getter-only properties
  • Properties with both getter and setter (should allow assignment)
  • Regular properties (should allow assignment)

Fixes #1094.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Add readonly property check for simple assignments (=) to property access expressions.
Previously only compound assignments (+=, -=, etc) were checked for readonly properties.

The issue was specifically with cross-module imports where the readonly check was
bypassed for named imports. Fixed by continuing readonly validation for non-namespace
alias symbols and calling checkPropertyAccessExpression for simple assignments.

Co-authored-by: RyanCavanaugh <[email protected]>
@Copilot Copilot AI changed the title [WIP] No error when assigning to getter-only static class property from another file Fix: No error when assigning to getter-only static class property from another file Jun 7, 2025
@Copilot Copilot AI requested a review from RyanCavanaugh June 7, 2025 05:30
Copilot finished work on behalf of RyanCavanaugh June 7, 2025 05:30
@RyanCavanaugh
Copy link
Member

Almost but not quite

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

Error loading sessions

Retrying...

Successfully merging this pull request may close these issues.

No error when assigning to getter-only static class property from another file
2 participants