3 ms·
By sub-type variance are you referring to issues like this https://stackoverflow.com/questions/47850513/unsafe-implicit-conversion-of-generics-in-typescript htt
by inbx0 5y ago
By sub-type variance are you referring to issues like this https://stackoverflow.com/questions/47850513/unsafe-implicit-conversion-of-generics-in-typescript https://stackoverflow.com/questions/47850513/unsafe-implicit... ?
FWIW the lack of explicit variance annotations (and probably many other TS unsound design choices) are not so much related to supporting old untyped JS, but rather language design decisions that have to do with pragmatism and type system complexity.
I can't find it now but I have a vague recollection Hejlsberg talking about the explcit variance annotations explicitly, how those were a huge can of worms in C# and they didn't want to bring them to TS. Unlike Flow, by the way, which does have explicit variance handling and doesn't suffer from the beforementioned foot gun (but does have its own foot guns of course).
- ragnese 5y agoYes and no. I do think that there are only two correct ways for a type system to do generic user-defined types: you either give us variance markers or you make all user-defined generic types invariant (Swift does this). Personally, I hate the latter, but not as much as I hate unsoundness. But, what I'm talking about is even more basic: https://www.typescriptlang.org/play?#code/MYGwhgzhAECCB2BLAtmEBvAvgKG6SMAIgPYDm0ApgB4AuF8AJjAimtOttF9AEZgBOAawAUASnaduU4MXgRiICgDoQZYQCIA7sWIAzdaMlccOPOCjQAwmBqVa9JnCSoQEqcgrFNYt1O4y5BWVVUg0PLwMjaBNcRHg6fl0wYApoAAlIAAUKWw4pAAccgC4nVhBsU10AV3hgGkRZaAgaKt1dYUL+eXgSjIhsmnE87k7upULbAF5oeApNKxsxCtwAehXoVDjY+IpE5NSAJQowBlkQAE8+knJhrn5j0-gL6AmS6+W8WWaNihKjk7Ol0g12g01uL2KMzm0GuS0w0Eg0ACzVW6wAygALYhVEAMaAASV4qTQqk0FDxNGILwICOg-0eFyuZGglNp1Vq9UaNAxNmgmjA8RgYHSWRyAH5sM1Wu0PIZsGtoAB1DHnFkY1LqkCuYACgDktmQqom0FO5D4QgR8HOyGI9zFTjxmhVJuIFBgiFsqgAbqkdTQkPkmlTkFVgBiJR5xjklOaROIFfwavUPJR+PxbQBCIA https://www.typescriptlang.org/play?#code/MYGwhgzhAECCB2BLAt... The code I wrote there is actually wrong for two reasons: 1. Type variance 2. Readonly doesn't do fuck-all in TypeScript for object types
- inbx0 5y agoYeah I agree, that's not what I'd consider good variance rules either. I guess my point was more about the reasons behind these weird rules. AFAIK interop with existing JavaScript was not the primary reason for doing them this way, if that's any consolation. FWIW your example wouldn't fly in Flow https://flow.org/try/#0PQKgBAAgZgNg9gdzCYAoVBjGBDAzrsAQQDsBLAW2xgG8BfdLPAgETgHMwBTADwBdPiAEwIkKVMNVRhpYAEbYATgGsAFAEoJUmdoxxiuODE4A6eGxUAiBHDhQLardPr1MOfGADC2Xlz4DhRGSUMJra5JyI6qHaMrr6hiZmluGI9o5gzuikxPwKUNgYnGAAEngACpw+ktIADpUAXIFiMKguUACuxBi8pHpguLztUFAqdQoGxI2luBW8GtUyYxPGdT4AvGDEnEhevOqt6MDAYJTZWTmceQVFAEqc2IJ6MACe06wcC9IA1KuN7weYPQDE6cRp3B5PV54d5gDafMC-TbbMDvfYuI5gADKAAs4O0YIIwABJORFKjwBCcQm8OAIphgbBgcGPYgvN7sMA0hlgDpdHp9XjY7xgBDYHIERnTWYAflQAyGI3CDlQGIA6tjnpzsUVtTAQhgxQByHzkTWrMCPDjyZQM4jPchwBScaWBQkIDUWuCcAikHzwABuRQNvDINX6tPI7Qw2Nl4RWlWM1tUGgxCk6PXCXAUCkdAEIgA https://flow.org/try/#0PQKgBAAgZgNg9gdzCYAoVBjGBDAzrsAQQDsBL...
- ragnese 5y ago> I guess my point was more about the reasons behind these weird rules. AFAIK interop with existing JavaScript was not the primary reason for doing them this way, if that's any consolation. Are you sure? Because there was an essay written by the TypeScript devs when they released whatever version that introduced the strict-function-variance flag explaining why they "couldn't" make it also apply to class methods, and it boiled down to breaking too much existing JavaScript code. That's not proof that the original variance choice was also due to JavaScript compatibility, but I feel like it's a very strong suggestion that it was probably a big motivator. I'm glad that it wouldn't work in Flow. I'm sure it wouldn't work in most/all of the other statically typed compile-to-JavaScript languages. I just hate that none of them won out over TypeScript, so they're basically all irrelevant- Flow included.