4 ms·
Java would absolutely stop you from returning a JSON object when you're supposed to return a Thing. You could cast it but you'd still get a ClassCastException.
by xxpor 6y ago
Java would absolutely stop you from returning a JSON object when you're supposed to return a Thing. You could cast it but you'd still get a ClassCastException. Now I haven't written JS or TS, so maybe I'm not understanding what you're saying?
- damagednoob 6y agoJust like Generics in Java, Typescript only exists at compile time. Talking about what Typescript doesn't do at runtime is moot.
- lmm 6y agoTypescript permits you to write code that does implicit unsafe casts at runtime. Type-safe languages don't. Java Generics don't let you implicitly do unsafe casts[1]; erased generics mean the unsafe cast wouldn't necessarily fail visibly at runtime (though there are warnings and linters to alert you to that, and techniques to work around it), but it would enforce that it was explicit in the code (and you would then know to add extra test coverage etc.). [1] You can implicitly do unsafe casts using arrays, but arrays aren't really using generics, they're a special-case builtin.
- winrid 6y agoThe example above is equivalent to gson.toJSON(string, Thing.class) It'll just return an empty Thing right? No ClassCastException.