-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Erroneous control flow type narrowing in loops #15835
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
Comments
Note that neither of these variants reproduce the bug: // Variant 1
const enum Escape {None, Any}
function scan(): void {
let escape = Escape.None as Escape;
while (true) {
escape = escape === Escape.Any ? Escape.None : Escape.Any;
}
}
// Variant 2
function scan(): void {
let escape = false;
while (true) {
escape = escape === true ? false : true;
}
} My current workaround is casting the const enum Escape {None, Any}
function scan(): void {
let escape: Escape = Escape.None;
while (true) {
// No error here
escape = (escape as Escape) === Escape.Any ? Escape.None : Escape.Any;
}
} Also, note that in my code, I have about 8-10 cases, for a |
I just noticed what I believe is the same bug, but without the loop: enum MyEnum {
Foo,
Bar
}
class MyClass {
private state: MyEnum;
// Set state to Bar
private modify(){
this.state = MyEnum.Bar;
}
public fn() {
this.state = MyEnum.Foo
this.modify();
if (this.state === MyEnum.Bar) {
// Do stuff here...
}
}
} This produces the following error
Just tested this with Seems that typescript doesn't think that variables of enum type can ever change? |
@JShabtai Yours is subtly different:
|
Works now. Other comment is a dupe of 9998 |
TypeScript Version: 2.3.2
Code
Expected behavior:
The type to be correctly broadened, since it's in a loop.
Actual behavior:
The error denoted in the code comment above the line in question.
The text was updated successfully, but these errors were encountered: