4 ms·
> What thing that violates a type check would be "perfectly fine to do"? Here is an example limitation of typescript's type system that I routinely run into wh
by seepel 6y ago
> What thing that violates a type check would be "perfectly fine to do"?
Here is an example limitation of typescript's type system that I routinely run into while developing real code [1].
I look at it like this. If you consider all possible programs, some are invalid, some are valid, and some are valid and useful. A type system's job is to reject as many invalid programs as possible while accepting as many valid programs as possible and trying to optimize for useful valid programs. Due to the halting problem this is impossible to do perfectly, so any given type system will likely accept some invalid programs and reject some useful valid programs. If the type system happens to reject your useful valid program, you'll likely have a bad day :)
[1] https://www.typescriptlang.org/play?#code/PTAEHUFNQCwQwG7TgO1HADhgNgSwMZwAuuA9mkfEaACaRzYDOoA7rpaJOzJAE6gBlIgFc6KIs1L8AYnHzDsRAJ4A6AFC5xfAGZzoQ0ZHGgA3mtCcAtnFzYAXKEZFemgObnQrjHAdOXKdwBfNQ0tXl18aFl5RSVTD0hrW19nNw9GBjheJRT-IJCQUABJbVZoFlRqIlJOAA9nOSqeKxtsUG1eUkt0T07hDFBSUqdDcUl+CIVlUEpoNULZwYAjBDJhZkZSbGESclAl3noAa2YlyEJ16EWAA2sMa9BLSEpSGnR8EQZsOKI4I8hmHBQPMwIRsNglnIjqxuDMYIdoN5eHAnkQ+ICUG9FpRcLw3rhmIt8FtsOddmh2IxINhtGptMIUB8yGhXM8AKJJJgAClcfQwDgAgrxkUoADwGMREAB8oAAPqAhSLRdEpkopQBKeIWQ4iXgsvkqO5crkYdHkBwSozUeUq2KagC8MtNvE2KBUiVa6rUwRBxQoMAJoEIVLKQdQnme6D6mLhgbwlnYxGZ+3OcEuofI31A6cWvuuHtsD20uGpNDpDKZe1ZRA5rUYACYeXyLSJJQBtAC6ctAtuUnc1Zm1z2EetATdI-XQzEVcDFJgL9kcqQCgQ1hswxudrodTrNboXXp9hSK1lcmmgmic9DeLHKlUgWJqLGRA3o+BgywAVmSYRxFpo6FqUBNDhaBeQnDB1E0NFwj0P1AIfUUABUZUHYDMUgWoHBQYRLDOXgPHYRIHCQ70QnpRlyXQwCAHE+WQqVx36QVhVnBj1RYpUigw2pEJQ1CPB1Ed9Qg9cMGNIjLAAGmozCdy1CxQCE0cTFkrC1JkySHEk0BggsQJDwKMA6HwbAsmgRhcEsHAfmpJhQJmJRTUYfAXAwahiWs2w+DhYgegQLJcDgJZSTqAkJF9Ng-2aRhTXwXBiwfRzTXUEyzMOINyCcJdRgkTi2MtcQpTUNLzMylBssmWJ8rFXs1RK850ugYkKuoEZJUYAB5Xg6pbXLO27OrO19KAwxQaB02uADMLoiCHjgZhsHIVwp1DI4UFIFhWB4WZ+EWLIRXK34Kt9dqrXGdo5FVUJaL5LkzrGL1ptqWb+i5KrlC9Y9SlvWhyAAcmodbNscOKEoIL44hYAN32Sq5mgO2cjpsCrQycWxsF9QgUA2jyvlAKaeNe+5OCQf0J1cD9duQDKNveT42mUU1OGFKRGCgom7oeiRurq9UgA https://www.typescriptlang.org/play?#code/PTAEHUFNQCwQwG7TgO...
- temp24582934 6y agoSometimes the type system is forcing you to think in terms of what it is that you are passing around. In this case, getEmails() isn't expecting a group of students or a group of faculty, but rather a group of people that can be emailed. You can introduce an interface of that type and have it inherited by both Student and Faculty and use that in the method for clarity and type-safety without over-relying on union types: https://www.typescriptlang.org/play?ssl=1&ssc=1&pln=38&pc=30#code/JYOwLgpgTgZghgYwgAgArQM4HsTIN4BQyyEAtnMADYBcyGYUoA5gQL4EGiSyIoDKYAK4ATCOBIAPSCGEY0mHPiLImABzi16jEC3adw0eEmQAxRIMpgAnpOmz5UbLkLEMcSnChXNDZmw4wgiAIYMCKTBBgAKLkVBgAFExQWIKqtACCUFBwVgA86I44AHwAlErEUJGCULhJKaoAdOSq8fGqCiC0BU5lALxFyO2FIA1kFJQl-vrcRigAkjIQEhDCuQAqAy7IoKIStCCCpABG0MrAkKS0a1OBwaGKO0sA4smp60WJr2nImdl5GyUMlkcrkFrsVu9NspKkIaiovk04C14ucyAAabaLCR9KHECpVOF4TG7WiPCQY1GXbYXZDsYisSZ6UQIDyVZAIHD0OhCUTgDBAv65AQiMRgIoEZmslAckBcowWawCkFmBAKqziyWeaWcsDckV8gDyUBVaqVeW6xQ4EWisUoCXo+rAGEm1pi4wS8ssVhdkTdcXiDt5TqNJq9k30uxe9QDPNFzojzy+8U91kmZKjqRjjowIfMYaAA https://www.typescriptlang.org/play?ssl=1&ssc=1&pln=38&pc=30...
- dmohs 6y agoI appreciate the effort you’ve both put in to concrete examples. I think yours gets to a point that I haven’t often seen stated. You’ve named your interface Person, but perhaps even Emailable could serve the purpose. The problem is that you had to name it. Naming well is hard, and I believe strong type systems often create a need for more names. It’s a cost I don’t often see considered in the tradeoff.
- dmux 6y agoInstead of giving the interface an explicit name, I think you could instead use something like this: function getEmails(group: Array<{email: string}>) { return group.map((p) => p.email) } which leaves the function more open ended than relying on an explicitly named interface that other types then have to inherit from.
- deleted 6y ago[deleted]
- deleted 6y ago[deleted]