Hacker News new | ask | show | jobs
by nathias 1709 days ago
Everything that TS does JS libraries do better without the horrible tradeoffs ... sadly MS has invested so much into promoting it that it's now almost a requirement for all software development.
1 comments

> Everything that TS does JS libraries do better

Which ones do you need to do everything TS does?

> without the horrible tradeoffs

Which tradeoffs are horrible?

Not OP but I do tend to avoid TS. I don't like the additional friction of working with the language (transpiling, unable to copy/paste directly into an interpreter). I also feel like the community at large writes awful baroque code that makes me want to die. Why use a function when 18 classes subclassing eachother across 4 files will do? If you're familiar with the tiktoker @khaby.lame, TS feels like exactly the over-complicated life hacks he mocks.
I've found the perfect middleground is typescript checking with JSDoc syntax. The code is just JS, no emitting necessary, but you get all the type checking.

Together with testing of @example stanzas, you get everything for cheap-ish.

Alas, there is no good JSDoc @example test runner¹. This would be very valuable. If there were, I would let that handle my unit tests and focus only on integration testing.

Edit: runtime type checking at the boundary is also valuable! I've tried runtypes, but actually prefer compiling the ts definitions to JSON Schema with typescript-json-schema, and checking with plain JSON Schema validation.

¹I've used @supabase/doctest-js and jsdoctest, and found both lacking. If you know of a better one, please share!

TS !== crazy OOP. For some reason, there are people who just want to use Java style OOP in TS. Nest.js is one library heavily promoting that. I can't digest such codebases, they are the definition of over-engineering to me. Fortunately, out of dozens of medium to big TS projects I worked on, only one used that style. In all the other projects there were very few if any classes.
You can use `ts-node` to copy paste directly into an interpreter. (`npm/pnpm install -g ts-node`)
Fair, I should have been more clear when I said "interpreter" what I really meant is "the chrome debugger" which is one of JS/Node's absolute killer features. I believe support there is on it's way, though I very much doubt it'll convince me of TS' value.

I used to be much more bullish on TS, I like the idea of strong typing in general but the more I use TS the more I feel like it's just the worst of both worlds. I much prefer my types to be deeply embedded in the language design, types as varnish don't make sense to me anymore.

You can very easily use chrome debugger on sourcemapped TS files
This is news to me, as far as I know it'll error out when it reads TS on the REPL. How do you enable this?
Exactly, this plus adding a new layer of complexity and a new corpo sponsored replacement for something that isn't broken ...
No one using TS claims JS is broken. People want a good type system in javascript, and with that obviously comes a new layer of complexity

The tradeoff is clearly worth it for certain use cases, and clearly not worth it for others. It seems you are discounting and ignoring cases when it is worth it.

For example if you want prop types you use propTypes. In React TS replaces good error handling for horrible obscure errors and slows down development considerably etc. etc.
propTypes does runtime type checking, which is a different kettle of fish from static type checking.

The advantage to static type checking is that it removes the performance cost of runtime type checking where it's unnecessary; the language's rules make it impossible to build some constructs where the wrong types get mashed together. The tradeoff is that you have to code so the wrong types don't get mashed together (which is, arguably, your goal in the first place).

You can do everything a statically-typed language does in a non-statically-typed language via best practices, but that's a bit like saying you can do everything a compiled language does in assembly via emulating what the compiler would output. In theory, the compiler is saving you the headache of doing that (but depending on the size of what you're trying to write, sometimes it is simpler to write it in JavaScript and skip the type safety. That code is harder to grow, but not all code grows!).

So the whole baroque arhitecture is there to 'remove performance costs'? That's an even worse reason to use TS than avoiding prop type bugs.
This is a good response to drill down on, because the poster's assessment of the purpose is accurate.

Yes, much of both TypeScript and React are there to minimize performance costs and improve software reliability in tens-of-thousands-of-lines-of-code projects: TypeScript is using static type safety to replace the need for dynamic typechecking (decreasing the expected runtime error rate and the runtime cost of dynamic type analysis; static typing tells you both when you must runtime-coerce types and when such coercion is unnecessary and would waste performance). React is using delta-detection of lighter-weight objects to determine when heavier-weight objects in a declarative user interface API need to be changed, impacting performance (because a handful of equality compares against objects or plain data is a fraction of the cost of repainting every pixel in a table with pixels representing the exact same information as before to the end user).

These are problems people face, but if they are not the problems you're facing, they might not be the tools you need. Not everyone is writing the Facebook UI. There are lighter-weight tools out there that solve similar problems with less complexity (the tradeoff, perhaps, being that if you do find your software needing to scale to handle updates to represent complex, heterogeneous data or infinite streams of information, those tools might not scale easily... But how many people actually have that problem?).

"Use the right tool for the job" is one of the cornerstones of the art of software engineering.

Yes, the right tool for the job, but because TS covers a set of tools its rare that this is exactly the tool you need. I'm sure there are cases but mostly I think people just use it because of the hype/preference and I really hate to make prototypes with TS.
I'm really glad proptypes are not used anymore, ts is simply better. It's more flexible, gives stronger guarantees in some cases (PropTypes.func does zero checks for signature, for example), better support in editors, better integration with other libraries, allows typing hooks/context. If you need runtime checks, use io-ts or runtypes.