-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Description
π Search Terms
invalid constant type narrowing
π Version & Regression Information
Bug is visible on 5.9.1-rc1 as well as 4.9.5 (tested on typescript playground)
β― Playground Link
π» Code
class Foobar {
private data: boolean = false;
private func() {
this.data = false;
this.otherMemberFunc();
// is deduced as constant type `false` incorrectly even though the control flow flow can obviously mutate the variable
this.data;
}
private otherMemberFunc () {
this.data = true;
}
}
π Actual behavior
Typescript deduces the member type as constant even though a later member function call in the class can very easily mutate the data member and therefore invalidate the constant type narrowing.
π Expected behavior
Typescript should discard any type narrowing on class data members when calling member functions after the type narrowing.
I.e. in the posted code example this.data
should again be deduced as a boolean
instead of constant false
after calling the member function since the member function might definitely mutate any data accessible through this
in a class and therefore invalidate any type narrowing on member variables done before.
This might be a complex problem overall since this will extend to nested objects and arrays which are accessed through this
in a class member;
Additional information about the issue
No response