DEV Community

Discussion on: Cool stuff with Typescript

Collapse
 
functional_js profile image
Functional Javascript • Edited

Excellent refresher on some Typescript features Amine.

I don't use .ts files, but I get near all the services of TS with JSDocs, while allowing the code to remain pure JavaScript.

TS also willl not give runtime checks, as it downpiles to untyped JS.

To use your lowercase example above, to enforce runtime checks (where the productions bugs are found), I use this idiom....

/**
@func
lowercase all chars in a str

@usages
in pipes
- instead of method chaining

@param {string} s
@return {string}
*/
const getLowerCase = s => throwIfNotStr(s) || s.toLowerCase();
Enter fullscreen mode Exit fullscreen mode
Collapse
 
werasmus profile image
Werner Erasmus • Edited

Compile time checks are always better than runtime checks. Interesting obsession. Given that argument would be typed, the only way it could fail is if you lied to the compiler at some point. Typically that might be the point of deserialization... Test your inputs in one place and then trust your compiler. Also, types show intent. JSDoc isn't a type checker (or is it?)