Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

If you disagree, you're welcome to prove me wrong!

To give you an example from a popular open source ts-heavy project:

https://github.com/elysiajs/elysia/blob/94abb3c95e53e2a77078...

The `return this as any` there, which effectively casts it to the same type this had, but with the added get route is perfectly safe, it works, and will never be a problem by itself.



The `return this as any` in this codebase was chosen because it was the easiest/quickest route not because it was necessary.

Tbh I'm not paid by this company enough to spend time breaking down the correct solution but it would involve validating and discriminating the result of `this.add`.

This code is NOT type-safe and will be harder to maintain. Someone will make a change in the future that changes the possible results of `this.add` and TypeScript will not be able to warn you about the consequences of that change.


It’s funny because the reason you used a language like typescript is because you want the compiler to be 100% sure that it’s compatible, not relying on human reasoning.

If you were going to rely on that anyway, why not just use JavaScript as is and avoid the boilerplate from typescript


The code I linked for example results in a web router that is fully type safe.

It's not like using js at all, not that I think there's anything wrong with it, if that's your jam.


Because typescript’s type-checking helps with 99.99%% of the code, and then 0.01% of the time when it doesn’t you use an escape hatch.


> It’s funny because the reason you used a language like typescript is because you want the compiler to be 100% sure that it’s compatible, not relying on human reasoning.

I ever used Typescript because I hoped it would catch some bugs that I didn't (and it did). I never expect it to be 100%. I can't even imagine 100% of what.


This is a great example of "letting perfect be the enemy of good enough".

Typescript is "good enough" at it's job. That's a great reason to use it.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: