-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
base: main
Are you sure you want to change the base?
Conversation
Co-authored-by: jakebailey <5341706+jakebailey@users.noreply.github.com>
…t issue Co-authored-by: jakebailey <5341706+jakebailey@users.noreply.github.com>
@typescript-bot test it |
This comment was marked as off-topic.
This comment was marked as off-topic.
@jakebailey Here are the results of running the user tests with tsc comparing Something interesting changed - please have a look. Details
|
This comment was marked as off-topic.
This comment was marked as off-topic.
Hey @jakebailey, the results of running the DT tests are ready. Everything looks the same! |
This comment was marked as off-topic.
This comment was marked as off-topic.
@jakebailey Here they are:
tscComparison Report - baseline..pr
System info unknown
Hosts
Scenarios
Developer Information: |
This comment was marked as off-topic.
This comment was marked as off-topic.
@jakebailey Here are the results of running the top 400 repos with tsc comparing Something interesting changed - please have a look. Details
|
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:
The issue was in the
getAssignmentTarget
function insrc/compiler/utilities.ts
, which traverses up the AST to determine if an identifier is the target of an assignment. The function correctly handledParenthesizedExpression
andNonNullExpression
by continuing the traversal, but was missing cases forTypeAssertionExpression
(angle bracket syntax like<any>x
) andAsExpression
(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:
(<any>x) = value
as
syntax:(x as any) = value
((x as any) as unknown) = value
(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.