microsoft / microsoft/TypeScript
Suggestion: stricter operators
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 132
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 justnumberandstring, 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: https://github.com/Microsoft/TypeScript/pull/7140
and in particular this rationale: https://github.com/Microsoft/TypeScript/pull/7140#issuecomment-186432250
This would fall under of "stricter" TypeScript, https://github.com/Microsoft/TypeScript/issues/274 .
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the Binary Operators section of doc/spec.md and the linked strictNullTypes discussion, then review the broader stricter-TypeScript proposal in issue #274. The work would be complete when an opt-in option enforces the proposed operand types for +, +=, - and -= while preserving any behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, typescript
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 30/100