-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Allow parenthesis in type guards #32130
Comments
Duplicate is correct - allowing this syntactically would not be sufficient to enable the behavior you're looking for |
@RyanCavanaugh Ok, from what I read in the issues this is mostly about control flow analysis performance. |
You posted this code:
This would not work with the way the control flow works today, even if your proposed syntax is allowed. Functions can not change the type of a variable within the current scope. Allowing that would require the changes in the control flow analysis. |
@Haringat You do use a new kind of flow analysis. Today a simple function call such as Typescript builds a graph of nodes that can impact the type of variables. This graph includes nodes of constructs that can impact the type of a variable (such as This is why such a change would have a big performance impact, it would grow the CFA graph by a lot. |
@dragomirtitian ah I think I am starting to get it now. Thanks for the explanation. |
This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
Search Terms
type guard parenthesis
Suggestion
I would like to be able to put the asserting part of a type guard function in parenthesis to be able to further work with it.
Use Cases
I have two use cases in mind right now: strong-typed assertions and predicate inversion. However, there may be more that I currently don't think of.
Examples
Strong typed assertions
This would enable code like this:
This would make the following code possible:
It would also allow better detection of dead code/bugs:
Predicate inversion
Using a few more functional predicates we could do something like this:
Checklist
My suggestion meets these guidelines:
The text was updated successfully, but these errors were encountered: