DEV Community

Static Typing or Typescript

Jesse Phillips on February 14, 2021

This is in response to Article No Longer Available I have mixed feelings about Typescript. To quote Indian Jones, "you ...
Collapse
 
peerreynders profile image
peerreynders

I have mixed feelings about Typescript.

My issue is the simplistic notion among TypeScript devotees that TypeScript makes their code "safe" - often because they haven't been exposed to anything better than Java or C#.

  • Any remaining explicit or implicit any are punching holes into their safety net.
  • Even if all explicit or implicit any have been eliminated they are still dealing with the equivalent of a leaky barrel in terms of type safety (A Note on Soundness: "TypeScript’s type system allows certain operations that can’t be known at compile-time to be safe.").

Anybody who thinks that TypeScript "has their back" is living an illusion (and needs to try Rust for comparison).

Anybody who is actually serious about "type safety" would dismiss TypeScript out of hand and move to something like ReScript:

Has no pitfalls, aka the type system is "sound" (the types will always be correct). E.g. If a type isn't marked as nullable, its value will never lie and let through some undefined value silently. ReScript code has no null/undefined errors.

But there is a cost - JavaScript interop with something like ReScript is a lot more work.

But if interop is the primary concern, why did TypeScript introduce its own non-compatible syntax extensions? The limited value that TypeScript can actually offer can be easily derived from type annotations in comments (a la JSDoc TS - Reference) for occasional static analysis, leaving the (no-build-step-required) ECMAScript syntax intact.

The trouble with TypeScript:

TS is fully authortime convenience and really only obfuscates at runtime which isn't beneficial. (Non standard syntax aesthetics aside.)

FYI: there is actually an article on dev.to: The Trouble with TypeScript

Collapse
 
kayis profile image
K

My opinion too.

I read a paper [0] that analyzed how much bugs TypeScript really found and it stated something about ~15%, which isn't that much when take the struggles people have while actually using TS in their daily work.

Would be interesting to do these tests with ReScript.

[0] earlbarr.com/publications/typestud...

Collapse
 
jessekphillips profile image
Jesse Phillips

Sorry QA perspective, I don't really care that it is a struggle, I'll take 15%. Again how I feel about Typescript suggests I don't like it either, that doesn't mean I want javascript instead.

I think web developers brought this on themselves. Type systems are showing to be valuable in reducing bugs and the hatred for working with types means the winning system is the one that straddles both worlds so it can claim you can gradually add types.

Thread Thread
 
kayis profile image
K

I'm not saying static typing is bad. ReScript seems like a step in the right direction.

Thread Thread
 
peerreynders profile image
peerreynders

Sorry QA perspective, I don't really care that it is a struggle, I'll take 15%.

That "struggle" does diminish "return on investment". And opportunity cost means that effort invested in the struggle may be more effectively spent elsewhere.

Then there is the risk of adopting a technology that could be "end-of-lifed" at the drop of a hat. Microsoft and Google regularly stop supporting their technologies. Flutter is the only thing keeping Dart on life-support - so while the TypeScript user-community seems large enough it isn't clear that there is enough will, enthusiasm and critical mass to continue the product if Microsoft bows out.

Then there is the issue that while there may be a sufficient body of developers singing the praises of TypeScript in terms of "Developer Experience" only very few people seem to be evaluating the quality, volume, and performance of the code generated by the compiler even though they directly affect the non functional aspects of the end product - which can enhance or diminish the end product's core functionality.

Using type-aware tools doesn't replace learning about type-driven development (classes are no substitute for types) which typically requires working with something like OCaml or Haskell. Once you've been through that ringer even your JavaScript should be better (though type annotations and static analysis should be helpful).

Thread Thread
 
kayis profile image
K • Edited

Very good points.

Thank you!

Collapse
 
stereoplegic profile image
Mike Bybee • Edited

The multiple 800 pound gorillas that I have yet to see TypeScript supporters adequately address are:

  1. You don't need TypeScript (or any other "superset") to check JavaScript types statically. JSDoc @param/@typedef/@type/@return/etc. annotation, combined with ESLint (or VS Code set to check JS) works just fine. You don't even need to install JSDoc (though you get auto-generated code docs if you do) - it's just a docgen for a function/class comment syntax/doc paradigm that has been standardized across multiple languages.
  2. 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.
  3. TypeScript still transpiles to loosely, dynamically typed JS. Your struggles to appease tsc are moot.
  4. Junior developers are still trying to grok JS qua JS - i.e. loosely, dynamically typed. Not only is point 2 a major issue in this regard, but so are the mountains of extra learning curve you're piling on them... And then there's the most damning cop-out of TS not named "any": "typeof" (which is completely different in a TS context despite the same exact keyword existing in JS, with either context potentially occurring in a .ts/.tsx file. Super fun for someone still just trying to learn JS.
  5. Assumption that those criticizing TS are inferior developers who just had one or two bad experiences with it, and that there's no possibility whatsoever that the person criticizing TS might have used it longer than you have and seen its long term negative effects on complex, real world projects.
Collapse
 
jessekphillips profile image
Jesse Phillips • Edited
  1. I am not familiar with the TS alternatives, usually the issue I read about equally apply to introducing static typing in any form, see your 2, 3, and 5.

  2. I'm going to first need to restate what I think you are getting at.

It is important to do runtime testing for other important system failures, type issues will be identified during this effort.

Is that accurate?

Thus static type checks are redundant.

Is that also where you are headed?

.3. This is true in all static type languages, the types are a compile time construct. The TS to JS adds the complexity that while working in Typescript you may actually be working with untyped code and you've only declared expectation on that untyped code.

This is much like passing void* in C. The type system is subverted and the compiler will assume you know the type. It is not like C in that JS actually has runtime type information and you will only have corrupt data due to JS loose typing around strings and numbers.

.4. Why are you using Typescript and having your junior devs learning all the dynamic, loose typing. Teach them to stay in the typed world.

I'm actually being overly harsh here, I don't think Typescript sufficiently separates the two words so that you can avoid the other, this is likely the root cause of my mixed feelings. I need to learn how to get TS to help me rather than it providing guarantee I can use.

.5. If I utilize points 1, 2, 3 and 4 as a base. Point 4 is the only one that I would say hints at long term negative effects. Otherwise these issues read more like

'Why utilize Typescript when there are better alternatives. We don't use those because static typing is pointless.'

Put in point 5 and it is topped off with 'and you don't believe me even though I've used Typescript longer than you.'


I think static type systems have proven themselves over and over again. But we also have sufficient evidence that systems can be built without static typing with stability and reliability that an argument against static typing is strong.

As someone whose job it is to assure that stability, I don't want to be doing it for dynamic languages.

Collapse
 
stereoplegic profile image
Mike Bybee • Edited

I never said not to check types statically. Never said static type checks are redundant. I said you don't need anything but JavaScript and tooling you're already likely to use with it (with or without TS) for that. And that you still need to learn the dynamic type system JS already offers to actually safeguard against wrong types at runtime. TS adds zero benefit, and a great deal of burden, for things you already can do without it (and still need to do with or without).

Your response to point 5, in the context of your other interpretations of what I actually said, is "You still don't really understand TypeScript, no matter how long you've used it. How could you not accept its greatness and inevitability if you did?"

Your response to 4 ignores 3. They can't avoid dynamic typing. They're transpiling to JS. It also, as is far too common among TS fans, ignores what junior devs are already expected to learn (and anybody who tells them to ignore dynamic typing needs to go back and re-learn JS - sans TS - themselves).

Your responses to 4 and 3 prove my point about 2.

I don't want to be doing it for dynamic languages.

Then I guess you'd better start doing all of your frontend in WASM (loaded with plain ol' dynamic JS, with or without TS, of course).

Thread Thread
 
jessekphillips profile image
Jesse Phillips

My intention for my summary of 5 was thinking it might help you see how the sum of your statements come together.

Thank you yor try to do the same, though I need some help connecting the translation. Namely my mistake seems to primarily stem from the assumption that you are not in favor of types.

I would say this assumption mainly stems from item 2, which I explicitly asked for help in understanding. Could I get further clarification on how that point isn't general to all type systems mentioned?

Thread Thread
 
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.

Collapse
 
bias profile image
Tobias Nickel

i recently worked on a backend, there was a function that executes some sql query and groupBy the result. the function was used from 3 other places and we knew there was something odd. It was not wrong every time, just in certain situations.

The project is mostly in typescript, (at least all code in .ts files) but that function had any as return type.

after spcifying the type, typescript directly pointed to the actual bug we had.

So, while the typeScript typesystem is not perfect (still need validation libraries for real typechecking at runtime), I believe it is valuable to use it in projects.

Collapse
 
jwp profile image
JWP

Merge errors always are caught by
GIT. This makes the branch argument weak.

Introduction of Typing is a design decision. It should have already considered ensuring proper interface implementation.

Dynamic typing is nice but a potential liability. If we look at Graphql we see it requires type defs.

None of the issues cited have much to do with Typescript or Javascript. Its merely lack of knowledge of the all important interface of 2 systems; which should, at a minimum be documented.

Collapse
 
jessekphillips profile image
Jesse Phillips

Git only identifies content modification conflicts, it will not tell you the data no longer contains the property you are looking for. This relates more to private API were rigidity on defined APIs are more fluid, granted internal APIs can deserve the same rigidity.

I agree that the examples have system boundary issues, which is not language specific in fact mine was in C#.

People better understand the importance of defining their interfaces across systems, but like you did can easily dismiss the importance for internal interfaces as "git will catch it".

Collapse
 
jwp profile image
JWP • Edited

I think my git comment was in thinking of a new type in current branch where there are other non merged branches that didn't know about the new design change. I agree Git would never find that edge case.

The problem of 'it will not detect the property ' is true in Javascript and one pitfall that can cause headaches. Its the primary reason I prefer Typescript, where for example, it's impossible to misspell a property name. I have seen dynamic Javascript object property misspelling.

Yes, well defined interfaces are required but not always present. Any code like that is of poor quality.

Thread Thread
 
jessekphillips profile image
Jesse Phillips

Since my context is with test code, everything is internal. I've tried hard to use appropriate depreciation (is that even a thing in Javascript?) but I have had slip ups and it is nice to have a type system to keep things in check.

Collapse
 
jwp profile image
JWP

The soundness argument is weak given the example in the article. Why? Because the article clearly shows the mix of strong and dynamic typing, but then points out the compiler doesn't catch it. The real problem isn't the compiler. Its a result of mixing Typescript and Dynamic typing. Which in some cases is fine.

Collapse
 
jessekphillips profile image
Jesse Phillips

Maybe I misunderstood the soundness argument, I thought it was exactly what you stated. The type system is mixed with dynamic typing making it unsound.

Collapse
 
lukewestby profile image
Luke Westby

Something that I think gets overlooked in types vs no types is that you need a typed language if you have very strict performance requirements. Rust code achieves the speed it is known for only because the compiler can choose the fastest set of instructions based on what it knows about the types.

Collapse
 
peerreynders profile image
peerreynders • Edited

Just because there are types doesn't automatically imply that there is optimization for performance - which there isn't in TypeScript.

2017

Always bet on TypeScript

2020

Please don't open issues on JS repos asking why they aren't written in TypeScript :(

One of the reasons I find this an odd request is because I write my projects in JavaScript, but I generally type them.

Sure, it's TypeScript's checkJs option and JSDoc annotations, but it is typed.

TS is too indirect when file size is the primary constraint of a project, and I am completely unwilling to increase file size simply to make something strictly typed. To me, that's not a worthwhile compromise.

That is probably why some teams want TypeScript's annotations to be compatible with Google's Closure Complier.

Annotating JavaScript for the Closure Compiler