-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Type narrowing should be abandoned after an operation with potential side effects #13904
Comments
This seems conservative to the point of annoyance. It'd be very surprising to find a property narrowing reset after calling I would agree that it seems reasonable to invalidate narrowings on properties of an object which has a method called on it, though. It'd fix some really "stupid" narrowings we have ( Per usual see #9998 for other discussion |
I wonder if it would be reasonable to make a dependency graph within each block scope of which objects have access to which other objects? In the above example, |
The testcases in issues and #14695 and #19638 use getters. Given that statefulness is usually the raison d'etre behind a getter in the first place, then "is this property a getter?" would be a reasonable heuristic for selectively turning off CFA/narrowing. Anders's suggestion to silence the compiler is wrapping the property access in a function. With narrowing disabled for getters, the workaround would be to wrap it in a getter, which means (a) code that "reads" more idiomatically, and (b) eliminating the need to go edit every access to convert it into a function call instead. |
Needless to say this would be a massive breaking change, and incorrectly lots of correct code an error in ways that would be rather difficult to get yourself out of without type assertions. A proper fix would be some kind of mutability notation (similar to |
TypeScript Version: 2.1.1
Code
Expected behavior:
No errors, since this code is correct.
Actual behavior:
Operator '!==' cannot be applied to types '"before"' and '"after"'.
thrown on line 28.As put by @sethobrien: In the most extremely conservative compiler, it wouldn't do type narrowing on properties at all because the property getter itself could have side effects. that's probably too risk-averse to be useful. But if any other function call comes after we've narrowed a property, we should lose the narrowing info.
The text was updated successfully, but these errors were encountered: