7 ms·
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
by orneryostrich 6y 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.
- recursive 6y agoI'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.
- xixixao 6y agoIf you want a (practically) sound type system, use Flow.
- ufo 6y agoIIRC some of those soundness holes are an intentional tradeoff to simplify the type system. http://users.soe.ucsc.edu/~abadi/Papers/FTS-submitted.pdf http://users.soe.ucsc.edu/~abadi/Papers/FTS-submitted.pdf
- tsimionescu 6y agoThis 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[].
- ameliaquining 6y agoOnly 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.
- yxhuvud 6y agoWell, 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.