No JS tool (that I know of) will warn you about these conditions being impossible. But they should. const foo = 'FOO'; if (foo[0] === 'B') {} // impossible if (foo === 'BAR') {} // impossible
Same as Flow's %checks then. It's not a very ergonomic way to define refinements, and you're limited to the simple syntax that it lets you define.
-
-
It's actually more flexible because you could write a runtime check for a very complex object by using duck typing and then add the `is` return type. However, it would be nice if TS just figured out the single typeof automatically like you mentioned.
-
TypeScript would then just trust that your type guard was accurate then. In a different model, by the time you returned a boolean in the type guard function, you can save the refinements made to the input variable. That way you have more safety.
- 2 more replies
New conversation -
Loading seems to be taking a while.
Twitter may be over capacity or experiencing a momentary hiccup. Try again or visit Twitter Status for more information.
he/him 