4 ms·
If it was really not a statically typed language, I'd expect this code to at least run. But it detects a type failure at compilation time despite the lack of a
by recursive 8y ago
If it was really not a statically typed language, I'd expect this code to at least run. But it detects a type failure at compilation time despite the lack of any declarations.
function foo() {
return 1;
}
let x = foo().substring(1, 2);
- coder543 8y ago> I'd expect this code to at least run It still compiles the code to JavaScript even with the error, and you can still run it, which makes this more of a warning than anything else. That warning based on type inference seems like a positive feature... because there's no situation in which that code is right. You can add `: any` to the function declaration and it will stop complaining, but I wouldn't contend that the language is not dynamic because it encourages you not to make a mistake.
- int_19h 8y agoThe distinction between "warning" and "error" is highly arbitrary anyway. In C++, you also get a warning rather than an error if you, say, return a reference to a local, or read from an uninitialized local. You still get a binary, and it still runs. But these are 100% bugs, which is why most projects enable treat-warnings-as-errors for a lot of that stuff in C++. I'd imagine it's common with TypeScript, as well.