Hacker News new | ask | show | jobs
by leononame 658 days ago
In this case, not really. TypeScript can't be sound because there is zero runtime type safety in JS. That you are able to do `as unknown as T` makes TypeScript unsound, but it's also an escape hatch often needed to interact with JavaScript's dynamic typing.
2 comments

> often needed

It's never needed, it's just often convenient for something quick and dirty. You can always write a guard to accomplish the same thing—roll your own runtime safety. If you want to avoid doing it manually there's Zod. It's not that much different than writing a binding for a C library in another language in that you're manually enforcing the constraints of your app at the boundaries.

> an escape hatch often needed

The only time you need that is in tests.

Otherwise you're better off using guards then blindly casting

The code base I work on has more test code than production code.