-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Suggestion: stricter operators #7989
Copy link
Copy link
Open
Labels
Effort: ModerateRequires experience with the TypeScript codebase, but feasible. Harder than "Effort: Casual".Requires experience with the TypeScript codebase, but feasible. Harder than "Effort: Casual".Help WantedYou can do thisYou can do thisSuggestionAn idea for TypeScriptAn idea for TypeScript
Milestone
Metadata
Metadata
Assignees
Labels
Effort: ModerateRequires experience with the TypeScript codebase, but feasible. Harder than "Effort: Casual".Requires experience with the TypeScript codebase, but feasible. Harder than "Effort: Casual".Help WantedYou can do thisYou can do thisSuggestionAn idea for TypeScriptAn idea for TypeScript
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
--strictNullCheckson.2 + 'a'=>"2a"null + 'a'=>"nulla"(!)2 - null=>2I propose letting users opt in (maybe via some
--strictOperators) to strict operator behavior. Concretely I think this means:restrict
+, and+=to justnumberandstring, e.g. for the former only declarerestrict
-and-=to justnumber(
anyshould 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
strictNullTypeschange: #7140and in particular this rationale: #7140 (comment)
This would fall under of "stricter" TypeScript, #274 .