3 ms·
To be fair, that's valid in Typescript because it simply uses Javascript's coercion rules. You could imagine it having built-in rules for JS' coercions: s
by hombre_fatal 8y ago
To be fair, that's valid in Typescript because it simply uses Javascript's coercion rules.
You could imagine it having built-in rules for JS' coercions:
string + number -> string
number + string -> string
Maybe your snippet is provocative to some people, but in real code it would quickly fail once you actually use `bar` and were wrong about your assumptions.
At which point it's similar to languages with `Int + Float -> Float` and `Short + Long -> Long` in that perhaps you wish those operators weren't defined but it's at least consistent. And you'd find the error once you've passed those results to a function that expected Int or Short.
- chc 8y agoThis is more of a knock against the compromises TypeScript's wacky type system has to make for JavaScript compatibility than anything else. It's not possible in a language that was really designed for strong static typing like Haskell or Rust.
- pas 8y agoIt's not the fall, but the stop that kills you. So in case of TS we don't know what was the intent. Maybe it was this. function suffixWith10(s: string) { return s + 10; } But if the intent can be discovered by analysis of usages of the variable later in the code, then TS will scream loudly.