Hacker News new | ask | show | jobs
by orneryostrich 1992 days ago
It seems to me like TypeScript's soundness hole in #4 could be fixed.

1. I'd want TS to show an error on the line where the aliasing occurs, encouraging me to clone the list if I want to change its type from a distance.

4 comments

I'd be concerned that any fix that still follows typescript's design principles would do more harm than good. In practice, the unsoundness of typescript doesn't seem to cause much actual problem, although more than none. It's easy to create example programs that demonstrate the problems, but they seem to be comparatively rare in day to day code.
IIRC some of those soundness holes are an intentional tradeoff to simplify the type system. http://users.soe.ucsc.edu/~abadi/Papers/FTS-submitted.pdf
This soundness hole (or a very similar one) was also accepted in Java itself (with subtypes rather than sum types).

The main reason behind accepting it is that it is very useful and safe in certain situations that the type system is too weak to define strictly: if a function takes an (string|number)[] and only reads from it, it is perfectly safe to pass in a string[].

Only arrays (the only generic type that predates Java 5) have this problem in Java, and for precisely this reason, modern Java practice avoids arrays in favor of other collection types. When real generics were introduced in Java 5, they used a design with proper variance that doesn't have this problem.
Well, that depends on context and language. I know at least on language where the binary representation of String and String|Int is different and where the difference matters.
If you want a (practically) sound type system, use Flow.