4 ms·
Typescript is flawed, but it is a godsend compared to using straight JS. You can use it like Java until you can't. And then you are forced to muck around with J
by codesushi42 7y ago
Typescript is flawed, but it is a godsend compared to using straight JS. You can use it like Java until you can't. And then you are forced to muck around with JS hacks just to please the transpiler.
Usually that happens using generic types. They are almost completely worthless in Typescript, and have no analog whatsoever in JS. So you are mucking around just to please the transpiler but adding a type unsafe hack anyway in place of a generic argument.
Typescript should have just stuck with providing checks on type declarations. That's it. Stop adding features from Java/C# when they make no sense in JS.
I would pick another language in a heartbeat, given a choice.
- nicoburns 7y agoTypeScript's type system is actually much more expressive than Java or C#'s. You can't express OR types (sum types) like `string | number` nicely in those languages. It's the main thing that made me hate statically typed languages until I tried Rust (which also lets you express this).
- codesushi42 7y agoHuh? You would just overload the method then in another language. That is a lot safer than manually checking the argument type inside the method with the hack you proposed. You're right if by "more expressive" you meant "not really safe at all". By using the types of hacks you proposed, you end up losing type information and safety guarantees. So why bother using Typescript in the first place then? Your example is the perfect argument against Typescript. If you were referring to generic types, then there's no good reason to do that in the first place. Use a base type. Polymorphism 101. Anything else would be duck typing, and not safe. There's no middle ground with type safety: it is either safe or not. And if there's a chance it's not, then you've lost and all of your efforts are pointless without resorting to manual checks. And if you were somehow referring to C++ like templating (unlikely), then Typescript doesn't support that anyway.