Skip to content

Fix assignment-target detection through type assertions - #64965

Open
Cheng (chengchuu) wants to merge 2 commits into
microsoft:mainfrom
chengchuu:fix/assertion-assignment-targets
Open

Cheng (chengchuu) wants to merge 2 commits into
microsoft:mainfrom
chengchuu:fix/assertion-assignment-targets

Conversation

@chengchuu

@chengchuu Cheng (chengchuu) commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fixes #62127

Recognize assignment targets through as and angle-bracket assertions so the Go compiler no longer reports false TS2454 ("used before being assigned") diagnostics on plain writes. This follows #62128 and adds the const and readonly assignment tests requested by Jake Bailey and Ryan Cavanaugh. This example now compiles without errors.

function example() {
  let value: number;
  (value as number) = 1;
}

Genuine uninitialized reads still report errors. Assignment checks now catch writes through assertions to constants, readonly properties, class bindings, and restricted generic indexes. The resolutionCache.ts fixture casts the cache object to a mutable type so it can still update the readonly property in place.

A read after an assertion-wrapped assignment can still report TS2454.

Validation

The fixture covers plain and compound writes through assertions, uninitialized reads, and invalid assignments.

Commit Change Focused test result
affbdd7c79 Adds regression tests and corrected baselines Failed as expected
0835223177 Fixes assignment-target and access detection All passed

I ran these tests from the repository root:

go -C tsc test -mod=readonly ./internal/testrunner ./internal/fourslash/tests \
  -run '^(TestLocal|TestQuickInfoOnAssertionAssignmentTarget)$/^(usedBeforeAssignedTypeAssertion|classNameReferencesInStaticElements|assertedReadonlyParameterPropertyAssignment|assertedGenericPartialIndexedAssignment)\.ts$' \
  -count=1 -v

The standalone reproduction reported TS2454 before the fix. It compiled without diagnostics using a compiler built from 0835223177.

AI assistance: Copilot helped generate the regression tests. I reviewed the changes and will handle any review feedback or revisions myself.

Copilot AI balanced review requested due to automatic review settings October 26, 2026 05:55
@github-project-automation github-project-automation Bot moved this to Not started in PR Backlog Oct 26, 2026
@typescript-automation typescript-automation Bot added For Backlog Bug PRs that fix a backlog bug labels Oct 26, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

No active review issues remain for the assertion-wrapped assignment-target change.

No open issues 🎉

Review effort: Balanced

@chengchuu
Cheng (chengchuu) force-pushed the fix/assertion-assignment-targets branch 4 times, most recently from d77b20f to 66c1d58 Compare December 4, 2026 10:20

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

No active review issues are reported for the assertion-wrapped assignment-target change.

No open issues 🎉

Review effort: Balanced (adaptive)

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

For Backlog Bug PRs that fix a backlog bug

Projects

Status: Not started

Development

Successfully merging this pull request may close these issues.

"used before being assigned" fires on LHS inside parens/type assertion

2 participants