3 ms·
> it was a meme that no one's C program ever compiled the first time they tried Ah but see, the compiler is a guard-rail. Technically, it's a static-analyzer.
by _bxg1 8y ago
> it was a meme that no one's C program ever compiled the first time they tried
Ah but see, the compiler is a guard-rail. Technically, it's a static-analyzer. I don't think anyone is against making existing guard rails more helpful via editor integration; the conflict comes with adding new guard rails. Take the following example:
let foo = {
bar0: 12,
bar1: 14,
bar2: 16
}
// example 1
console.log(foo.bar0);
console.log(foo.bar1);
console.log(foo.bar2);
// example 2
for(let i = 0; i < 3; i++) {
console.log(foo['bar' + i])
}
Example 1 can be statically verified by a JavaScript type checker. Example 2 can't. There are advantages of being able to do the second thing - more code reuse, possibly less refactoring needed. But it's impossible to verify that those properties exist on that object without running the code. The argument is that oftentimes, the type checking is more valuable than the loss of "creative" freedom. But it does mean limiting the kinds of things you can do.
- Zarel 8y ago> Ah but see, the compiler is a guard-rail. Yes, I agree. It's a guard-rail which is missing in scripting languages like JavaScript and Ruby, and which some people are resisting adding. > Example 1 can be statically verified by a JavaScript type checker. Example 2 can't. With type assertions, it's not really limiting at all. For instance, I'd write example 2 as: for (let i = 0; i < 3; i++) { console.log(foo['bar' + i as keyof typeof foo]); } If you weren't as experienced with TypeScript, you can also just throw around `any` assertions until the problem goes away: let foo: any = { bar0: 12, bar1: 14, bar2: 16 }; Or if you wanted to be tricky, you could even just assert a specific key: for (let i = 0; i < 3; i++) { console.log(foo['bar' + i as 'bar0']); } It's true that there are a lot of things typecheckers can't check. But that's no reason to give up on the things they _can_ check.
- recursive 8y agoIn case anyone missed it, example 2 is allowed in typescript.
- Zarel 8y agoIt's not, not without an assertion: https://i.imgur.com/DJjzPDF.png https://i.imgur.com/DJjzPDF.png Well, it's allowed in that it will still compile the code (because TypeScript is a superset of JavaScript), but it'll still yell at you.
- recursive 8y agoYes, without an assertion. Just change your compiler options. https://www.typescriptlang.org/play/#src=%20%20let%20foo%20%3D%20%7B%0D%0A%20%20%20%20bar0%3A%2012%2C%0D%0A%20%20%20%20bar1%3A%2014%2C%0D%0A%20%20%20%20bar2%3A%2016%0D%0A%20%20%7D%0D%0A%0D%0A%20%20%2F%2F%20example%201%0D%0A%20%20console.log(foo.bar0)%3B%0D%0A%20%20console.log(foo.bar1)%3B%0D%0A%20%20console.log(foo.bar2)%3B%0D%0A%0D%0A%20%20%2F%2F%20example%202%0D%0A%20%20for(let%20i%20%3D%200%3B%20i%20%3C%203%3B%20i%2B%2B)%20%7B%0D%0A%20%20%20%20console.log(foo%5B'bar'%20%2B%20i%5D)%0D%0A%20%20%7D%0D%0A%20%20 https://www.typescriptlang.org/play/#src=%20%20let%20foo%20%...