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
-
-
And it should work across files, and shouldn't require a %checks annotation like in Flow.
Show this threadThanks. Twitter will use this to make your timeline better. UndoUndo
-
-
-
TS can do this with user-defined type guards: https://www.typescriptlang.org/docs/handbook/advanced-types.html …
-
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.
- 4 more replies
New conversation -
-
-
This is possible with TS...you need to add a return type to your helper function though : str is string Try it here: https://goo.gl/sWewNu
New conversation -
-
-
In TS is something like User-Defined Type Guards. Example
function isString(foo: string | number): foo is string {
return typeof foo === 'string'
}
Usage:
if (isString(s)) {
// here TS knows it is string
} else {
// here TS knows it is number
}Thanks. Twitter will use this to make your timeline better. UndoUndo
-
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 