-
Notifications
You must be signed in to change notification settings - Fork 12.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Suggestion: stricter operators #7989
Comments
number + string is actually quite common in JavaScript land and will probably not happen as it moves the null + string should be disabled as a part of Note:A number of |
related #7746 |
@basarat I really struggle when the argument "because that's how Javascript does it" is applied. Particularly in cases like this where I think the cognitive load on the developer is increased by decisions to stick to the Javascript way. Not because it's unclear how let s: string;
let n: number;
s = n; // ERROR: number is not assignable to string
n = s; // ERROR: string is not assignable to number
let res = s + n; // OK: really? I understand that |
@mhegazy because as I understand it, there is no coercion when it comes to |
@Aleksey-Bykov do you agree that this suggestion encompasses the one in #7746? |
i agree, thank you for considering |
Approved behavior change: under |
Thanks for looking at this! That behavior change would be most welcome! Do you have any thoughts on the other bits of the request (in particular string + number)? Also, I guess `${string}${nullable}` is still legal for debugging purposes even if |
We don't want the string template syntax to be different from the basic concat rules in terms of type system behavior; one is just sugar for the other and it'd be weird to have different rules. |
The null part, yes. I am still a little disappointed that type checking is basically disabled if the expression involves a string, e.g. this is legal and there's no opting out:
|
For tslint users, we have https://palantir.github.io/tslint/rules/restrict-plus-operands/ Edit: this won't help for template strings, at least not yet: palantir/tslint#3670 |
seems already fixed |
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 declarerestrict
-
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: #7140and in particular this rationale: #7140 (comment)
This would fall under of "stricter" TypeScript, #274 .
The text was updated successfully, but these errors were encountered: