You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Ryan Cavanaugh (@RyanCavanaugh) Ok, from what I read in the issues this is mostly about control flow analysis performance.
However, in the above example I did not use new kind of control flow that is not already in typescript (basically the above examples are a combination of conditional types and type predicates) so I do not understand why each one individually is accepted in the language but the combination is not. Edit: Or rather, why the combination would lead to an unacceptable type checking performance drop.
However, in the above example I did not use new kind of control flow that is not already in typescript
You posted this code:
function doSomethingWithStrings(it: unknown) {
assertString(it);
return it.startsWith("foo") // it must be a string now
}
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 assertString(it); can't impact the type of it. With your proposal it could impact the type of it.
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 if, switch statemenets, assignments etc). With this change ALL calls would have to be included in this graph (the graph is constructed when type information about the function is not yet available, so there is no way to know that assertString has an impact on CF while other functions don't).
This is why such a change would have a big performance impact, it would grow the CFA graph by a lot.
Activity
MartinJohns commentedon Jun 27, 2019
Duplicate of #10421, #31512, #31879, #17760.
RyanCavanaugh commentedon Jun 27, 2019
Duplicate is correct - allowing this syntactically would not be sufficient to enable the behavior you're looking for
Haringat commentedon Jun 27, 2019
Ryan Cavanaugh (@RyanCavanaugh) Ok, from what I read in the issues this is mostly about control flow analysis performance.
However, in the above example I did not use new kind of control flow that is not already in typescript (basically the above examples are a combination of conditional types and type predicates) so I do not understand why each one individually is accepted in the language but the combination is not. Edit: Or rather, why the combination would lead to an unacceptable type checking performance drop.
MartinJohns commentedon Jun 27, 2019
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.
dragomirtitian commentedon Jun 27, 2019
Haringat You do use a new kind of flow analysis. Today a simple function call such as
assertString(it);can't impact the type ofit. With your proposal it could impact the type ofit.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
if,switchstatemenets, assignments etc). With this change ALL calls would have to be included in this graph (the graph is constructed when type information about the function is not yet available, so there is no way to know thatassertStringhas an impact on CF while other functions don't).This is why such a change would have a big performance impact, it would grow the CFA graph by a lot.
Haringat commentedon Jun 27, 2019
Titian Cernicova-Dragomir (@dragomirtitian) ah I think I am starting to get it now. Thanks for the explanation.
typescript-bot commentedon Jun 29, 2019
This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.