Nearly all the type errors I see are things like
var foo = "Hello World";
bar = foo + 10;
That should scream it's head off.Nearly all the type errors I see are things like
var foo = "Hello World";
bar = foo + 10;
That should scream it's head off.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.
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.
var foo = 5;
var bar = 100;
baz = foo + bar;
Why is that bad? Well... what do 5 and 100 mean in that program? Did you just add age in years to pixels from edge? How do you know?Appending an integer to a string is comparatively sensible next to adding age in years to pixels from edge.
Appending a number to a string is a perfectly sensible thing to do when you're displaying information to a user, yet adding two numbers or concatenating two strings could be completely senseless and proof that the development process has gone off the rails.
My dream type system is strict about semantics and only handles representation in specific circumstances. The simplest way to talk about this is in terms of units of measure: Height is a type. Whether it's in inches or centimeters is a representation. Whether that representation is a number or a string is another representation. Keeping track of whether you're showing height-in-inches or height-in-centimeters to a user is useful, but burdening your mind with whether this height-in-inches is a string or a number right now when the runtime can just as easily convert between the two is senseless, and fretting over height-in-inches versus height-in-centimeters adding person-height to shoe-sole-height to get height-in-shoes is similarly senseless.
I've yet to see a strictly/strongly typed language that makes this easy to do. Through some combination of plugins, esoteric languages, macros, or boilerplate, sure.
We're decades behind what is possible due to the practical.
You know because you should be using sensible variable names that make it obvious.