Hacker News new | ask | show | jobs
by staticassertion 1366 days ago
I agree with your point that TS is held back by being "just" a type checker. In particular, in Rust I can write code like this:

    let _: Result<Vec<_>, _> = some_iter.collect();
or like this:

    let _: Vec<Result<_, _>> = some_iter.collect();
The only thing that has changed here is the type annotation. Both will compile, but the implementation of `collect` is determined by that type. As far as I know this isn't the case in typescript, although please tell me if I'm wrong, I have limited experience from it and I'm thinking more from mypy perspective.

This ends up meaning that my types don't drive the program as much. There's this sort of declarative "here's the thing I want, use an implementation of that method to give it to me" that feels very powerful. I miss this a lot with mypy, my type annotations feel very much like annotations, they are a separate part of my program.

If TS were to lean into itself as its own thing, not just a way to do better JS, I think that'd be amazing. I'd love to see an AOT compiled TS where types are a semantic part of the program, I'd love to see it drop `any` entirely as well, and drop support for using JS in TS.

As for soundness, don't care too much tbh.

2 comments

> please tell me if I'm wrong

There's no exact equivalent, but you can use generic functions or function overloads to achieve something similar:

https://www.typescriptlang.org/docs/handbook/2/functions.htm...

Of course it won't select an implementation for you, so you'll have to handle all the possible type combinations yourself in the function body.

Yeah, that's pretty different. That's still the programmer having to think about types as a separate entity or a runtime construct.
Another annoying part of this is how every function declaration effectively doubles the list of parameters, once as type signature and once in the usual JS function syntax. And those don't even have to match, neither in length nor name of the parameters.