Skip to content

Fix "used before being assigned" error with type assertions in LHS #62128

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

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Jul 25, 2025

The TypeScript compiler was incorrectly reporting "Variable 'x' is used before being assigned" errors when variables appeared on the left-hand side of assignment expressions that were wrapped in type assertions or parentheses.

For example, this code would incorrectly error:

function test() {
  let blah: Something;
  
  beforeEach(() => {
    (blah as any) = class Wow {};  // ❌ Error: Variable 'blah' is used before being assigned
  });
}

The issue was in the getAssignmentTarget function in src/compiler/utilities.ts, which traverses up the AST to determine if an identifier is the target of an assignment. The function correctly handled ParenthesizedExpression and NonNullExpression by continuing the traversal, but was missing cases for TypeAssertionExpression (angle bracket syntax like <any>x) and AsExpression (x as any).

When these type assertion expressions were encountered, the function would return undefined instead of continuing up the tree, causing the identifier to not be recognized as an assignment target. This led to the "used before being assigned" error being incorrectly triggered.

The fix adds the missing cases to continue AST traversal for type assertion expressions, consistent with how other wrapper expressions are handled.

Test cases added:

  • Type assertion with angle bracket syntax: (<any>x) = value
  • Type assertion with as syntax: (x as any) = value
  • Nested type assertions: ((x as any) as unknown) = value
  • Parenthesized expressions (already working): (x) = value

All test cases now correctly recognize the assignments and don't produce "used before being assigned" errors.

Fixes #62127.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

@typescript-bot typescript-bot added the For Uncommitted Bug PR for untriaged, rejected, closed or missing bug label Jul 25, 2025
Copilot AI and others added 2 commits July 25, 2025 18:34
Co-authored-by: jakebailey <5341706+jakebailey@users.noreply.github.com>
…t issue

Co-authored-by: jakebailey <5341706+jakebailey@users.noreply.github.com>
@Copilot Copilot AI changed the title [WIP] "used before being assigned" fires on LHS inside parens/type assertion Fix "used before being assigned" error with type assertions in LHS Jul 25, 2025
Copilot finished work on behalf of jakebailey July 25, 2025 18:59
@Copilot Copilot AI requested a review from jakebailey July 25, 2025 18:59
@jakebailey
Copy link
Member

@typescript-bot test it

@typescript-bot
Copy link
Collaborator

typescript-bot commented Jul 25, 2025

Starting jobs; this comment will be updated as builds start and complete.

Command Status Results
test top400 ✅ Started 👀 Results
user test this ✅ Started 👀 Results
run dt ✅ Started ✅ Results
perf test this faster ✅ Started 👀 Results

This comment was marked as off-topic.

@typescript-bot
Copy link
Collaborator

@jakebailey Here are the results of running the user tests with tsc comparing main and refs/pull/62128/merge:

Something interesting changed - please have a look.

Details

arktype

/mnt/ts_downloads/_/m/arktype/build.sh

  • [NEW] error TS2540: Cannot assign to 'body' because it is a read-only property.
    • /mnt/ts_downloads/_/m/arktype/ark/schema/shared/compile.ts(58,10)

azure-sdk

/mnt/ts_downloads/_/m/azure-sdk/build.sh

  • [NEW] error TS2540: Cannot assign to 'delimiter' because it is a read-only property.
    • /mnt/ts_downloads/_/m/azure-sdk/src/util/pathUtil.ts(38,16)
  • [NEW] error TS2540: Cannot assign to 'posix' because it is a read-only property.
    • /mnt/ts_downloads/_/m/azure-sdk/src/util/pathUtil.ts(38,16)
  • [NEW] error TS2540: Cannot assign to 'sep' because it is a read-only property.
    • /mnt/ts_downloads/_/m/azure-sdk/src/util/pathUtil.ts(38,16)
  • [NEW] error TS2540: Cannot assign to 'win32' because it is a read-only property.
    • /mnt/ts_downloads/_/m/azure-sdk/src/util/pathUtil.ts(38,16)
  • [MISSING] error TS2322: Type 'string | ArrayBuffer | ArrayBufferView<ArrayBufferLike> | Blob | FormData | ReadableStream<Uint8Array<ArrayBufferLike>> | null | undefined' is not assignable to type 'BodyInit | null | undefined'.
    • /mnt/ts_downloads/_/m/azure-sdk/src/fetchHttpClient.ts(62,7)
    • /mnt/ts_downloads/_/m/azure-sdk/src/fetchHttpClient.ts(62,7)
  • [MISSING] error TS2322: Type 'Uint8Array<ArrayBufferLike> | Blob' is not assignable to type 'BlobPart'.
    • /mnt/ts_downloads/_/m/azure-sdk/src/util/concat.common.ts(18,5)
    • /mnt/ts_downloads/_/m/azure-sdk/src/util/concat.common.ts(18,5)
  • [MISSING] error TS2345: Argument of type 'string | ArrayBuffer | Blob | FormData | ArrayBufferView<ArrayBufferLike> | null' is not assignable to parameter of type 'Document | XMLHttpRequestBodyInit | null | undefined'.
    • /mnt/ts_downloads/_/m/azure-sdk/src/xhrHttpClient.ts(73,14)
    • /mnt/ts_downloads/_/m/azure-sdk/src/xhrHttpClient.ts(73,14)

This comment was marked as off-topic.

@typescript-bot
Copy link
Collaborator

Hey @jakebailey, the results of running the DT tests are ready.

Everything looks the same!

You can check the log here.

This comment was marked as off-topic.

@typescript-bot
Copy link
Collaborator

@jakebailey
The results of the perf run you requested are in!

Here they are:

tsc

Comparison Report - baseline..pr
Metric baseline pr Delta Best Worst p-value
Compiler-Unions - node (v18.15.0, x64)
Errors 34 34 ~ ~ ~ p=1.000 n=6
Symbols 62,370 62,370 ~ ~ ~ p=1.000 n=6
Types 50,386 50,386 ~ ~ ~ p=1.000 n=6
Memory used 193,603k (± 0.75%) 194,118k (± 0.96%) ~ 192,893k 196,557k p=0.471 n=6
Parse Time 1.31s (± 0.48%) 1.31s (± 0.62%) ~ 1.29s 1.31s p=0.599 n=6
Bind Time 0.73s 0.73s ~ ~ ~ p=1.000 n=6
Check Time 9.74s (± 0.34%) 9.75s (± 0.18%) ~ 9.73s 9.78s p=0.686 n=6
Emit Time 2.75s (± 1.12%) 2.74s (± 0.64%) ~ 2.72s 2.77s p=0.872 n=6
Total Time 14.52s (± 0.16%) 14.52s (± 0.19%) ~ 14.48s 14.56s p=0.747 n=6
angular-1 - node (v18.15.0, x64)
Errors 56 67 🔻+11 (+19.64%) ~ ~ p=0.001 n=6
Symbols 948,914 948,914 ~ ~ ~ p=1.000 n=6
Types 410,884 410,883 -1 (- 0.00%) ~ ~ p=0.001 n=6
Memory used 1,226,393k (± 0.01%) 1,226,343k (± 0.01%) ~ 1,226,231k 1,226,407k p=0.378 n=6
Parse Time 6.53s (± 0.74%) 6.55s (± 0.84%) ~ 6.50s 6.62s p=0.573 n=6
Bind Time 1.87s (± 0.28%) 1.88s (± 0.43%) ~ 1.87s 1.89s p=0.523 n=6
Check Time 32.06s (± 0.43%) 32.04s (± 0.39%) ~ 31.90s 32.22s p=0.936 n=6
Emit Time 14.82s (± 0.41%) 14.87s (± 0.45%) ~ 14.77s 14.95s p=0.333 n=6
Total Time 55.29s (± 0.26%) 55.34s (± 0.19%) ~ 55.20s 55.46s p=0.575 n=6
mui-docs - node (v18.15.0, x64)
Errors 0 1 🔻+1 (+ ∞%) ~ ~ p=0.001 n=6
Symbols 2,542,818 2,542,818 ~ ~ ~ p=1.000 n=6
Types 900,360 900,360 ~ ~ ~ p=1.000 n=6
Memory used 2,829,422k (± 0.00%) 2,829,236k (± 0.01%) ~ 2,829,011k 2,829,538k p=0.128 n=6
Parse Time 8.76s (± 0.39%) 8.75s (± 0.16%) ~ 8.72s 8.76s p=0.806 n=6
Bind Time 2.25s (± 0.46%) 2.25s (± 0.49%) ~ 2.24s 2.27s p=0.401 n=6
Check Time 85.88s (± 0.41%) 85.76s (± 0.13%) ~ 85.53s 85.84s p=0.575 n=6
Emit Time 0.62s (±124.72%) 1.28s (±76.41%) ~ 0.31s 2.24s p=0.059 n=6
Total Time 97.52s (± 0.84%) 98.04s (± 1.07%) ~ 96.84s 99.07s p=0.378 n=6
self-build-src - node (v18.15.0, x64)
Errors 0 2 🔻+2 (+ ∞%) ~ ~ p=0.001 n=6
Symbols 1,227,065 1,346,870 🔻+119,805 (+ 9.76%) ~ ~ p=0.001 n=6
Types 267,478 267,565 +87 (+ 0.03%) ~ ~ p=0.001 n=6
Memory used 2,363,252k (± 0.03%) 2,660,418k (± 0.01%) 🔻+297,166k (+12.57%) 2,660,036k 2,660,953k p=0.005 n=6
Parse Time 5.25s (± 1.25%) 8.03s (± 0.58%) 🔻+2.78s (+52.94%) 7.96s 8.09s p=0.005 n=6
Bind Time 1.79s (± 0.92%) 2.87s (± 0.65%) 🔻+1.09s (+60.97%) 2.86s 2.91s p=0.005 n=6
Check Time 35.39s (± 0.18%) 35.36s (± 0.20%) ~ 35.26s 35.46s p=0.470 n=6
Emit Time 3.00s (± 2.14%) 2.97s (± 1.24%) ~ 2.92s 3.01s p=0.748 n=6
Total Time 45.42s (± 0.28%) 49.25s (± 0.08%) 🔻+3.83s (+ 8.43%) 49.19s 49.29s p=0.005 n=6
self-build-src-public-api - node (v18.15.0, x64)
Errors 0 2 🔻+2 (+ ∞%) ~ ~ p=0.001 n=6
Symbols 1,227,065 1,346,870 🔻+119,805 (+ 9.76%) ~ ~ p=0.001 n=6
Types 267,478 267,565 +87 (+ 0.03%) ~ ~ p=0.001 n=6
Memory used 3,041,015k (± 9.78%) 3,338,215k (± 8.87%) 🔻+297,200k (+ 9.77%) 2,733,352k 3,459,728k p=0.045 n=6
Parse Time 6.92s (± 1.14%) 10.43s (± 0.73%) 🔻+3.52s (+50.87%) 10.32s 10.54s p=0.005 n=6
Bind Time 2.18s (± 1.97%) 3.51s (± 1.17%) 🔻+1.33s (+61.09%) 3.46s 3.57s p=0.005 n=6
Check Time 42.95s (± 0.31%) 43.11s (± 0.31%) ~ 42.92s 43.28s p=0.092 n=6
Emit Time 3.55s (± 2.61%) 3.50s (± 2.87%) ~ 3.35s 3.59s p=0.471 n=6
Total Time 55.59s (± 0.29%) 60.56s (± 0.27%) 🔻+4.97s (+ 8.94%) 60.28s 60.79s p=0.005 n=6
self-compiler - node (v18.15.0, x64)
Errors 0 1 🔻+1 (+ ∞%) ~ ~ p=0.001 n=6
Symbols 262,554 262,554 ~ ~ ~ p=1.000 n=6
Types 107,163 107,163 ~ ~ ~ p=1.000 n=6
Memory used 441,926k (± 0.03%) 441,971k (± 0.02%) ~ 441,841k 442,078k p=0.688 n=6
Parse Time 4.37s (± 1.00%) 4.38s (± 0.80%) ~ 4.33s 4.41s p=1.000 n=6
Bind Time 1.63s (± 0.85%) 1.62s (± 1.13%) ~ 1.59s 1.64s p=0.563 n=6
Check Time 23.44s (± 0.48%) 23.46s (± 0.18%) ~ 23.38s 23.49s p=0.809 n=6
Emit Time 1.89s (± 1.04%) 1.91s (± 1.03%) ~ 1.88s 1.93s p=0.368 n=6
Total Time 31.32s (± 0.47%) 31.36s (± 0.16%) ~ 31.31s 31.44s p=0.630 n=6
ts-pre-modules - node (v18.15.0, x64)
Errors 71 72 +1 (+ 1.41%) ~ ~ p=0.001 n=6
Symbols 225,367 225,367 ~ ~ ~ p=1.000 n=6
Types 94,290 94,290 ~ ~ ~ p=1.000 n=6
Memory used 371,186k (± 0.04%) 371,178k (± 0.03%) ~ 371,091k 371,312k p=0.810 n=6
Parse Time 2.89s (± 0.96%) 2.87s (± 1.15%) ~ 2.83s 2.92s p=0.374 n=6
Bind Time 1.60s (± 1.62%) 1.58s (± 1.29%) ~ 1.56s 1.62s p=0.318 n=6
Check Time 16.42s (± 0.27%) 16.43s (± 0.30%) ~ 16.38s 16.49s p=0.809 n=6
Emit Time 0.00s (±244.70%) 0.00s ~ ~ ~ p=0.405 n=6
Total Time 20.90s (± 0.33%) 20.89s (± 0.19%) ~ 20.83s 20.93s p=1.000 n=6
vscode - node (v18.15.0, x64)
Errors 1 3 🔻+2 (+200.00%) ~ ~ p=0.001 n=6
Symbols 3,553,654 3,553,654 ~ ~ ~ p=1.000 n=6
Types 1,198,187 1,198,187 ~ ~ ~ p=1.000 n=6
Memory used 3,598,194k (± 0.01%) 3,598,097k (± 0.00%) ~ 3,597,883k 3,598,219k p=0.575 n=6
Parse Time 15.21s (± 0.67%) 15.19s (± 0.43%) ~ 15.13s 15.31s p=0.574 n=6
Bind Time 4.97s (± 2.41%) 5.19s (±12.83%) ~ 4.90s 6.55s p=0.935 n=6
Check Time 99.58s (± 1.24%) 100.55s (± 3.81%) ~ 96.57s 107.91s p=0.689 n=6
Emit Time 34.48s (±20.74%) 32.85s (±10.05%) ~ 30.48s 37.83s p=0.471 n=6
Total Time 154.25s (± 4.45%) 153.79s (± 2.44%) ~ 150.17s 158.44s p=0.810 n=6
webpack - node (v18.15.0, x64)
Errors 2 2 ~ ~ ~ p=1.000 n=6
Symbols 321,021 321,021 ~ ~ ~ p=1.000 n=6
Types 140,135 140,135 ~ ~ ~ p=1.000 n=6
Memory used 477,323k (± 0.02%) 477,378k (± 0.02%) ~ 477,292k 477,511k p=0.630 n=6
Parse Time 4.31s (± 0.52%) 4.31s (± 1.03%) ~ 4.26s 4.38s p=0.687 n=6
Bind Time 1.80s (± 1.26%) 1.79s (± 2.01%) ~ 1.73s 1.83s p=0.746 n=6
Check Time 20.61s (± 0.57%) 20.61s (± 0.31%) ~ 20.54s 20.72s p=0.936 n=6
Emit Time 0.00s 0.00s ~ ~ ~ p=1.000 n=6
Total Time 26.72s (± 0.51%) 26.71s (± 0.25%) ~ 26.64s 26.84s p=0.936 n=6
xstate-main - node (v18.15.0, x64)
Errors 30 30 ~ ~ ~ p=1.000 n=6
Symbols 662,663 662,663 ~ ~ ~ p=1.000 n=6
Types 197,635 197,635 ~ ~ ~ p=1.000 n=6
Memory used 569,696k (± 0.02%) 569,816k (± 0.02%) ~ 569,564k 569,930k p=0.261 n=6
Parse Time 4.30s (± 1.15%) 4.28s (± 0.71%) ~ 4.23s 4.31s p=0.295 n=6
Bind Time 1.33s (± 0.95%) 1.33s (± 0.78%) ~ 1.31s 1.34s p=0.868 n=6
Check Time 20.35s (± 1.60%) 19.97s (± 1.38%) -0.38s (- 1.86%) 19.83s 20.53s p=0.020 n=6
Emit Time 0.01s (±167.16%) 0.00s (±244.70%) ~ 0.00s 0.01s p=0.527 n=6
Total Time 25.99s (± 1.40%) 25.58s (± 1.09%) ~ 25.42s 26.14s p=0.064 n=6
System info unknown
Hosts
  • node (v18.15.0, x64)
Scenarios
  • Compiler-Unions - node (v18.15.0, x64)
  • angular-1 - node (v18.15.0, x64)
  • mui-docs - node (v18.15.0, x64)
  • self-build-src - node (v18.15.0, x64)
  • self-build-src-public-api - node (v18.15.0, x64)
  • self-compiler - node (v18.15.0, x64)
  • ts-pre-modules - node (v18.15.0, x64)
  • vscode - node (v18.15.0, x64)
  • webpack - node (v18.15.0, x64)
  • xstate-main - node (v18.15.0, x64)
Benchmark Name Iterations
Current pr 6
Baseline baseline 6

Developer Information:

Download Benchmarks

This comment was marked as off-topic.

@typescript-bot
Copy link
Collaborator

@jakebailey Here are the results of running the top 400 repos with tsc comparing main and refs/pull/62128/merge:

Something interesting changed - please have a look.

Details

microsoft/vscode

6 of 60 projects failed to build with the old tsc and were ignored

src/tsconfig.tsec.json

build/checker/tsconfig.electron-browser.json

mui/material-ui

21 of 85 projects failed to build with the old tsc and were ignored

test/tsconfig.json

packages/mui-utils/tsconfig.json

packages/mui-system/tsconfig.json

packages/mui-material/tsconfig.json

packages/mui-material/test/typescript/moduleAugmentation/typographyVariants.tsconfig.json

packages/mui-material/test/typescript/moduleAugmentation/themeGetCssVar.tsconfig.json

packages/mui-material/test/typescript/moduleAugmentation/themeCustomNode.tsconfig.json

packages/mui-material/test/typescript/moduleAugmentation/themeCssVariables.tsconfig.json

packages/mui-material/test/typescript/moduleAugmentation/themeComponents.tsconfig.json

packages/mui-material/test/typescript/moduleAugmentation/textFieldCustomProps.tsconfig.json

packages/mui-material/test/typescript/moduleAugmentation/tabsCustomProps.tsconfig.json

packages/mui-material/test/typescript/moduleAugmentation/tableCellCustomProps.tsconfig.json

packages/mui-material/test/typescript/moduleAugmentation/systemTheme.tsconfig.json

packages/mui-material/test/typescript/moduleAugmentation/styleOverridesCallback.tsconfig.json

packages/mui-material/test/typescript/moduleAugmentation/reponsiveFontSizes.tsconfig.json

packages/mui-material/test/typescript/moduleAugmentation/progressProps.tsconfig.json

packages/mui-material/test/typescript/moduleAugmentation/paletteColors.tsconfig.json

packages/mui-material/test/typescript/moduleAugmentation/gridLegacyCustomBreakpoints.tsconfig.json

packages/mui-material/test/typescript/moduleAugmentation/gridCustomBreakpoints.tsconfig.json

packages/mui-material/test/typescript/moduleAugmentation/formHelperTextCustomProps.tsconfig.json

packages/mui-material/test/typescript/moduleAugmentation/createTheme.tsconfig.json

packages/mui-material/test/typescript/moduleAugmentation/buttonCustomProps.tsconfig.json

packages/mui-material/test/typescript/moduleAugmentation/breakpointsOverrides.tsconfig.json

packages/mui-material/test/typescript/moduleAugmentation/badgeCustomProps.tsconfig.json

packages/mui-material/test/typescript/moduleAugmentation/autocompleteCustomSlotProps.tsconfig.json

packages/mui-material/test/typescript/moduleAugmentation/appBarProps.tsconfig.json

packages/mui-material/test/typescript/moduleAugmentation/alertCustomSlotProps.tsconfig.json

packages/mui-material/test/typescript/moduleAugmentation/TooltipSlotSxProps.tsconfig.json

packages/mui-material/test/typescript/moduleAugmentation/InputLabelCustomProps.tsconfig.json

packages/mui-lab/tsconfig.json

packages/mui-joy/tsconfig.json

packages/mui-joy/test/typescript/moduleAugmentation/themeOverrides.tsconfig.json

packages/mui-joy/test/typescript/moduleAugmentation/themeGetCssVar.tsconfig.json

packages/mui-joy/test/typescript/moduleAugmentation/CssVarsProvider.tsconfig.json

packages/mui-icons-material/tsconfig.json

packages/mui-docs/tsconfig.json

docs/tsconfig.json

docs/scripts/tsconfig.json

packages/mui-joy/tsconfig.build.json

This comment was marked as off-topic.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
For Uncommitted Bug PR for untriaged, rejected, closed or missing bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

"used before being assigned" fires on LHS inside parens/type assertion
3 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy