microsoft / microsoft/TypeScript

Add the strictMathChecks option to allow for less complex code with math operations

Offen
#45,543 16 Kommentare 25 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Awaiting More Feedback Suggestion
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.4k
Ø Merge
1 T. 19 Std.
Gemergte PRs (30 T.)
117

Beschreibung

Suggestion

🔍 Search Terms

"undefined math"

✅ Viability Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

⭐ Suggestion

Typescript currently produces an error when comparing undefined or null in math operations. While this may be wanted in some situations, not everyone may want this and it has been a big source of errors in my code. I would like to suggest the addition of a tsconfig option called "strictMathChecks" that would default to true (current behavior) and, when false, would allow for comparison with undefined or nan in math operations.
The reasons why I believe this would make sense are the following:

  • The current alternative to this is doing Number(variable) on the variable that is producing the error, which turns it into NaN and can then be compared (always returning false) and produces the same result
  • In JavaScript, you can compare null/undefined in math operations and no error will be thrown, which does the same as using NaN like shown above.

Here are some short examples:

undefined > 50 // returns false in JS but throws an error in TS
NaN < 50 // returns false and does not throw an error in TS
null <= 100 // returns true but throws an error in TS

📃 Motivating Example

TypeScript will now allow you to use undefined/null values in math operations when the strictMathChecks option is set to false. This allows for less redundant code.

💻 Use Cases

As I explained above, the workaround for this error is pointless since this syntax is valid in JS, and applying it does not change the behavior at all, so it just becomes a minor inconvenience.

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Es sind keine Dateien, Tests oder Einstiegspunkte benannt. Beginne mit der Überprüfung von TypeScripts Behandlung von Compileroptionen und Diagnosen für undefined, null und mathematische Vergleiche; für den Abschluss müssten der Standardwert und das deaktivierte Verhalten der Option definiert und die aufgeführten Beispiele abgedeckt werden.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
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

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.