microsoft / microsoft/TypeScript
Suggestion: stricter operators
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Go
- Sterne
- 111k
- Forks
- 14.3k
- Ø Merge
- 2 T. 4 Std.
- Gemergte PRs (30 T.)
- 132
Beschreibung
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 .
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginnen Sie mit dem Abschnitt Binary Operators in doc/spec.md und der verlinkten Diskussion zu strictNullTypes, und prüfen Sie anschließend den umfassenderen Vorschlag für strengeres TypeScript in issue #274. Die Arbeit wäre abgeschlossen, wenn eine Opt-in-Option die vorgeschlagenen Operandentypen für +, +=, - und -= erzwingt und dabei jegliches Verhalten beibehält.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- javascript, typescript
- Bereich
- compilers
- Issue-Typ
- Feature
- Schwierigkeit
- 5/5
- Geschätzter Aufwand
- Über eine Woche
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 30/100