TypeScript is smarter than you in many of these regards and it's just looking out for you. I also ran into another thing with this the other day Typeguard which was I used a regular function but because functions are hoisted if there's a possibility that that could be called before you select the element so they have to check inside of the function again. One way to type guard is using a utility method so number dot is number that's actually not a method in JavaScript but you couldYou could go on NPM and grab a utility method You could use an is error method that specifically checks for it or is is not a number as well. If else statement if else statements will narrow for
In this episode of Syntax, Wes and Scott talk through some TypeScript Fundamentals around type narrowing, type guards, and type predicates.
Show Notes
Shameless Plugs
Tweet us your tasty treats