-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Don't error with "used before assigned" when declared type includes void #52054
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
Don't error with "used before assigned" when declared type includes void #52054
Conversation
@@ -27313,7 +27317,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { | |||
return convertAutoToAny(flowType); | |||
} | |||
} | |||
else if (!assumeInitialized && !containsUndefinedType(type) && containsUndefinedType(flowType)) { | |||
else if (!assumeInitialized && !containsUndefinedType(type) && !containsVoidType(type) && containsUndefinedType(flowType)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The problem is that the typeof
expression is the antecedent of the value
variable here and it changes the void
type somewhat unexpectedly.
narrowTypeByLiteralExpression discards void
from string | void
type for the assumeTrue === false
case here.
So when we get to this line here we have:
type // string | void
flowType // string | undefined
As the other antecedent pushes undefined
into the antecedentTypes
in the getTypeAtFlowBranchLabel.
I don't think this can be changed since this is how TS behaves today:
declare const a: string | void
if (typeof a === 'undefined') {
a // undefined
}
if (typeof a !== 'undefined') {
a // string
}
So checking against voidType
here was my best shot at fixing this.
EDIT: Alternatively... maybe the antecedents are set incorrectly here. I don't quite understand why value
is "linked" to this binary expression. I didn't dive into CFA implementation too much yet so I didn't even attempt to explore the possibilities there
FWIW: |
@fatcerberus ye, I realize that - but TS is very conservative about breaking changes, and apparently this is the current behavior: declare const a: string | void
if (typeof a === 'undefined') {
a // undefined
}
if (typeof a !== 'undefined') {
a // string
} That's why I've implemented this as a fix. Personally, I would prepare a different fix - one that wouldn't remove |
From reading #52003, I think the Not A Defect tag means that we don't want to fix that bug. So I think this PR should be closed? @RyanCavanaugh feel free to speak up if I interpreted Not A Defect incorrectly. |
FWIW |
Yeah, this is the kind of fix we need to reject if we don't want to bleed performance forever. People should not be declaring variables of type |
fixes #52003