Description
Currently operators like "+" are defined such that they match their semantics in JS. The below are all allowed by the compiler and produce the shown values, even with --strictNullChecks
on.
2 + 'a'
=>"2a"
null + 'a'
=>"nulla"
(!)2 - null
=>2
I propose letting users opt in (maybe via some --strictOperators
) to strict operator behavior. Concretely I think this means:
-
restrict
+
, and+=
to justnumber
andstring
, e.g. for the former only declarefunction +(a: number, b: number): number; function +(a: string, b: string): string;
-
restrict
-
and-=
to justnumber
(any
should continue to work as normal, of course.)
Relevant spec section:
https://github.com/Microsoft/TypeScript/blob/master/doc/spec.md#419-binary-operators
See also "Expression operators" in the strictNullTypes
change: #7140
and in particular this rationale: #7140 (comment)
This would fall under of "stricter" TypeScript, #274 .