re: How TypeScript squashed all known bugs VIEW POST

re: So your aTypeScriptMethod would have a strict typing of number which means it will only accept that type. This means when you later set age from d...

Put simply, typings tell the compiler what you think something is and it handles enforcement based on those assumptions. You think your incoming web service values are strings, so you define them that way and convert to number as needed when looking at the results.

Matt - I think you've misinterpreted the example problem: the string being passed in dataFromBackend.age can't be converted to a number; so your proposed solution could still cause a crash. The main problem lies here (my emphasis below):

You think your incoming web service values are strings

Actually you "think" your values are numbers masquerading as strings but you still don't have an absolute guarantee*; so you need to add some kind of guard in the code that converts to number to handle non-standard data. Without suitable handling your entire application could crash. As an extreme example I know of an OS map library that would crash your browser and require a reboot if you passed it a string instead of a number...

* except maybe if your backend is written in Typescript and shares the same type-definition

code of conduct - report abuse