DEV Community

Discussion on: Static Typing or Typescript

 
stereoplegic profile image
Mike Bybee • Edited

I'm against butchering JavaScript to behave like another language through the use of a "superset" (TypeScript, though I have the same issue with CoffeeScript, ClojureScript, etc.) because someone decided that, rather than take the time to learn JavaScript qua JavaScript, one should impose one's personal language preferences on all other devs involved.

In his original edition of You Don't Know JS, Kyle Simpson said two very important things when it comes to JavaScript:

  1. They're not "bad parts" or "weird parts," just JavaScript parts. Learn the what and why of these parts, not just the "good parts" (referring to the books JavaScript: The [Good/Bad/Weird] Parts)
  2. If you try to hack around or avoid these key characteristics of JavaScript (primarily referring, at the time, to the "object oriented JavaScript" craze of the early-mid last decade), you're setting yourself up for failure.

To be clear, what I'm saying is:

  1. Yes, you should check types at "compile" time, but you don't need anything other than vanilla JS, long-standardized comment syntax, and ESLint (and/or VS Code Intellisense set to check JS), to do that - none of the extra, bloated, nonstandard syntax or tooling of TS is necessary.
  2. That still doesn't save you from from needing to understand, account for, and utilize dynamic typing in JavaScript at runtime, whether or not you're writing it as TypeScript (though if you are, you're piling on a ton of unnecessary stuff in avoidance the core JS stuff you still need to learn, like it or not).
Thread Thread
 
jessekphillips profile image
Jesse Phillips

This does not help me understand your second point, which I'm still interested if I was able to extrapolate the meaning of correctly or not.

Thank you for providing a clearer position.

I think it is interesting that you don't like that someone is trying to make Javascript usable for themselves, yet are aware that the environment does not allow for an alternative, WebASM still has JS boot straps and is a much more recent option (not fully ready) compared to all previous attempts.

Thread Thread
 
stereoplegic profile image
Mike Bybee • Edited

Static type checking does nothing for "type safety" in runtime, creating a false sense of security when you still need to validate (ideally on frontend for user experience as well as backend for data security/integrity) in runtime.

I'm not sure exactly how I can further clarify this, other than to say "yes, you should check types statically, but you don't need TypeScript to do so" and "but you still need to account for runtime, which static type checking will never solve, and vanilla JavaScript already provides tools for this if you bother to learn its dynamic type system instead of acting like a static checking superset eliminates this need."

Making something useful for yourself is one thing. Imposing it on others and acting like it's the only way is something entirely different. Just look at all the demands on library maintainers to add types to their libraries, when the maintainers themselves haven't expressed any desire to use (let alone maintain additional support for) TypeScript.

Thread Thread
 
jessekphillips profile image
Jesse Phillips

Static type checking does nothing for "type safety" in runtime [...] yes, you should check types statically

I think I'm having a hard time with these two statements, why would you believe we should check types statically if at the same time you believe they do nothing for runtime. I think we both agree that the only thing that matters is runtime behavior. Everything else is done in support of getting the runtime portion correct.

I might be in agreement on pushing you desire for Typescript onto 3rd party libraries. I feel as though our discussion is more focused on why Typescript is or is not valuable and why it is winning the static typing challenge of javascript.

Thread Thread
 
stereoplegic profile image
Mike Bybee

Both of these can be true at the same time:

  1. Static type checking reduces some errors (not as many as TS proponents would have you believe), BUT you don't need TypeScript to check them statically in JavaScript, and completely altering the language is pointless if you don't need to.
  2. Static type checking isn't panacea, and you still need to account for runtime. Vanilla JavaScript already provides the tools for this.

In either case, TypeScript is a pointless "solution."

Thread Thread
 
jessekphillips profile image
Jesse Phillips

Static type checking does nothing for "type safety" in runtime [...] Static type checking reduces some errors

Why are you utilizing an absolute if you know it does do something, and you find that something valuable enough to us static type checking?

I'm not mentioning Typescript because your original statements seem very specific to static typing. That there where other options to Typescript were made in point 1 and I expected that was a different issue you had.

Thread Thread
 
stereoplegic profile image
Mike Bybee • Edited

It's not either/or. Check/annotate your types statically, but don't pretend (as TS users all too often do, and as you seemed to indicate earlier in the thread by saying you didn't want to deal with dynamic types) that you don't need to check in runtime and that JS doesn't already offer the tools to do so.

That "does do something" is easily solved for with just comments (that TS advocates tend to label "a code smell," oblivious to the irony of adding tons of nonstandard syntax instead) to document functions and their types, which has been around in the vanilla JS world since long before TS and other "superset" "solutions" to typing, and ESLint (which you should be using in a modern JS development toolkit anyway).

The "other option" I'm recommending is the one which alters JavaScript the least (i.e. not at all, rather just implementing good authoring practices).

I AM focusing on TypeScript (because your article is in response to one against TypeScript and) precisely because its supporters act as if it solves things which are impossible in vanilla JavaScript (I've shown a million times in this thread that they're not) and that their resulting code is safer and better organized. It's not, and I'd invite them to see the long-term (as in multi-year) effects on code "organization" vs a similar well-commented (and still self-documenting) JS codebase. In addition to the far fewer LoC, the JS codebase will benefit from devoting the hours (amounting to months in a multi-year project) TS devs would have wasted on satisfying tsc (just to get their code to run) on actually validating in runtime (again, necessary with or without TS), with time left over to spare.

Thread Thread
 
jessekphillips profile image
Jesse Phillips

I don't have the background on JS or TS to grasp gaps I expect to be closed with types, my little experience suggests TS does not go far enough. And for that I can believe TS is not good.

Basically what you say here I have no argument against, but maybe some semantics.

Two parts you emphasize are "vanilla javascript" and "standard javascript".

And while I agree that Typescript is not standard/vanilla javascript, I think you might be conserned more on the native browser support.

Personally I would not consider comment annotation as vanilla javascript. As to if it is standard, I would need to do a in depth analysis of JS projects I neither have time or access to.

Linters are great and I hope that any project using vanilla JS is making use of both of these techniques.