Fix: No error when assigning to getter-only static class property from another file #1095
+7
−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.
This PR fixes an issue where TypeScript-Go failed to report errors when assigning to getter-only properties imported from another module.
Problem
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
:Missing readonly check for simple assignments:
checkAssignmentOperator
only calledcheckPropertyAccessExpression
withwriteOnly=true
for compound assignments (+=
,-=
, etc.), but not for simple assignments (=
)Early return for named imports:
isAssignmentToReadonlyEntity
returned early for named imports instead of checking if the target symbol was readonlySolution
Minimal 7-line fix with two changes:
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 assignmentsFixed alias handling: Modified
isAssignmentToReadonlyEntity
to only return early for namespace imports, allowing readonly checks to proceed for named importsTesting
✅ 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:
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.