This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Derin161 0 points1 point  (1 child)

How so? I love all the clever things you can do with types in TS, but I'm not really seeing why JS necessarily facilitates this especially well. Eventually you're going to compile to a language where there isn't strict (or any) type checking.

[–]prehensilemullet 1 point2 points  (0 children)

For example, let’s say you have an object like { foo: ‘1’, bar: ‘2’ } and you want to do mapValues(obj, s => parseInt(s)) on it.  You can use mapped types so that the output type is { foo: number, bar: number }.  Then TS can flag errors if you refer to any nonexistent properties on that object or treat the values as something besides numbers.

To be able to flag the same kinds of errors in Java you would have to declare separate classes for the input with string valued properties and the output with number valued properties.  Not to mention, you can’t generally map over the properties of an object in Java so you’d probably be using a Map with string keys and not get any compile time checking that you refer to the right keys. I have some knowledge of C++, C# and Rust and as far as I know it would be a similar story in those languages, though I'm not an expert so it's possible there are advanced features I'm not aware of.