A Tiny Typescript Rant
For the last couple of weeks I have been using TypeScript and React Native for my job. I was tasked with creating a quick demo app to present to our clients. This is the first time I have used TypeScript in a real project and I honestly do not see the hype around the language. I mean, it's a slight improvement on JavaScript, but other than that, I'm not moved by it.
I can admit that typing adds some structure to my code and makes it easier to reason about what my program is doing, but other than that, I do not see any benefit of me using it over JavaScript. The positives I get from TypeScript can be achieved if I just use a linter, add some duck typing, and throw in a good type checker.
I do not know why people talk about it like it's the best thing to happen to JavaScript. I fail to see the appeal, maybe because I have never worked on a large-scale JS codebase and do not know the pain of debugging such programs. I think that I could get the same benefits of TypeScript with just the few tools I mentioned above.
The type system
The type system in TS is not all that; I can escape hatch myself away from it pretty easily using the any and unknown types. The compiler also allows pure JS to be written. This creates two problems, the first being that the program is only as safe as my teammates and I want it to be, and secondly, it allows me to create type-unsafe code just like I would in normal JS.
An example of this was when I was fetching data from an SQLite database. Initially I just sent out the query and used the result normally like any variable. It was only the next day in the morning when I was rereading my code that I realized that the result of my query had the unknown type. Even worse was that I did not handle the case in which the query failed. I had written an unsafe program that could fail, and the compiler did not even warn me about my mistake.
My application is only as safe as I wanted it to be, and the compiler does not provide guarantees for me. This could be bad if people are working on a large code base and no one is actively checking the quality of the code being written. An unsafe program could be written, compiled, and ran.
Sins of the Father
The other issue for me with TypeScript is the use of exceptions and null values. I know this is a problem with JS and not inherently with TS, but I cannot get over how some functions throw exceptions when they fail, and I can never know that from the type definition unless I read the documentation about that function.
This happened when I was looking for a decoder library to make my database request safe. I happened to find a small package that decoded JSON that I could repurpose. After going through the docs, I realised when the decoder failed, it just threw an error. This had me sprinkle try/catch clauses all over the place. But luckily I found Zod, and I really like the API it provided.
I think the major problem here is that I've recently been programming with Gleam, and I've had an amazing experience with it. Algebraic data types, exhaustive pattern matching, and a compiler that guides you when you make typing errors come standard, and now I have to deal with all these things myself. It's almost like getting into a new relationship and realizing your old partner was pretty great and you really took them for granted.
Below is a function I wrote to compose a bunch of functions together. I was proud of what i did for about 20 mins until i realised that this was an unreadable piece of trash that people will have a hard time reading and understanding. I need more experience writing Typescript!!!!
function compose<Type>(fns: ((a: Type) => Type)[], initial: Type) {
// used to chain functions together
return fns.reduce(
(prev_output: Type, currentFn: (a: Type) => Type) => currentFn(prev_output),
initial
);
}
Side note: This is very unstructured, and I wrote this on my lunch break out of pure frustration with using Typescript. I do not think it is a bad language; I just do not see why so many people advocate for the language as much as they do.