-
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
union types + type guards are extremely awkward with loops #4040
Comments
It seems like we should be type guarding the third clause of a |
third clause is a major pain point: the right side of it should be guaded, but the left should be not:
|
Thinking about it some more, another problem is that we're never going to apply a type guard to something that's assigned to, so we'd need #2388 first to be able to remove that restriction. Anyway, for simplicity's sake, I would write this: function fold<a, r>(values: List<a>, result: r, fold: (result: r, value: a) => r): r {
for (var node = <Node<a>>values; hasAny(node); node = <Node<a>>node.previous) {
result = fold(result, node.value);
}
return result;
} |
sure thing you can, it's just upsetting that unions and guards are effectively useless upon arrival, hope you guys can think something up |
Does this work as hoped with the new flow analysis? |
yes |
Consider an implementation of a list and a
fold
function in TS1.6:Now in order to iterate through such list in JavaScript all it would take is:
So I would expect that something like this could be easily done in TypeScript using its latest safety features like type guards and union types.
However what I ended up with doesn't look nearly as simple:
Now anyone who sees the generated JavaScript would call the cops on me.
Question: Is this how union types + custom guards + loops were designed to work together or I am missing something?
The text was updated successfully, but these errors were encountered: