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
-
Show this thread
-
Further, this should also be a warning: const nums = [1, 2, 3]; const num: number = nums[4]; // impossible, no bounds check
3 replies 0 retweets 5 likesShow this thread -
For arrays of unknown length, the type should be `?T`. You can support refinements of `arr.length` to avoid a majority of these.
3 replies 0 retweets 4 likesShow this thread -
Also refinements should work with function calls: function isString(str) { return typeof str === 'string'; } declare var foo: string | number; if (isString(foo)) { foo; // foo has been refined to string here } else { foo; // foo has been refined to number here }
4 replies 0 retweets 12 likesShow this thread -
Replying to @sebmck
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
1 reply 0 retweets 0 likes
Sebastian Retweeted Sebastian
Sebastian added,
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 