Hacker News new | ask | show | jobs
by justsomeuser 1346 days ago
I like type systems as you can design with types and check things tie together before even running the code. This gives you very fast iteration cycles compared to CMD-R refreshing the browser/code.

Some issues with TS are:

- There is no hard guarantee at runtime as everything is partially/optionally typed (including your deps).

- The types are not used to produce faster code at runtime (like in a compiled language).

It is almost like TS leaves half of the benefits of type checking on the table, obviously by design by being a JS superset.

3 comments

> There is no hard guarantee at runtime

You can get very close if you place fine-grained, well tested, type guards at IO boundaries, wrap poorly typed or overly dynamic dependencies, and have good discipline about internal boundaries. It’s a lot of work up front in a greenfield project, but it really pays off.

> The types are not used to produce faster code at runtime

Not directly, but in my experience good up front interface design tends to produce either immediately optimal JS (eg it tends to be monomorphic) or makes optimization much easier (as it makes any refactoring easier).

Edit:

> I like type systems as you can design with types and check things tie together before even running the code. This gives you very fast iteration cycles compared to CMD-R refreshing the browser/code.

I’d be remiss not to emphasize this! And not just skipping the reload cycle, it lets you skip a whole lot of test runs too. Type error in editor? Yep, those tests are gonna fail too.

>> - There is no hard guarantee at runtime as everything is partially/optionally typed (including your deps).

Most typed languages don't have hard guarantees at runtime. With java, using reflection you can do all kinds of weird things at runtime. C++ has fewer guarantees because there is no VM and can't even introspect objects to see their real type. Dependencies are a real issue with TS, but many libraries are written in typescript and the type files are really robust.

>> The types are not used to produce faster code at runtime (like in a compiled language).

The V8 intepreter will actually do this at runtime. Usually it takes less than a second for v8 to optimize the javascript, and it will recognize that many functions and objects use the same interface and optimize appropriately. If you think about it the types only help so much because once you have the code graph, it becomes really easy to find the type and optimize the memory layout.

Validating JSON against TS types avoids most runtime type errors, I’ve found.