microsoft / microsoft/TypeScript

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

オープン
#45,543 コメント 16 件 リアクション 25 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

Awaiting More Feedback Suggestion
主要言語
Go
スター
111k
フォーク
14.4k
平均マージ
1日 19時間
マージ済み PR(30日)
117

説明

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.

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

ファイル、テスト、エントリポイントは指定されていません。まず、undefined、null、数学的な比較に関する TypeScript のコンパイラオプションの処理と診断を確認してください。完了するには、オプションのデフォルト値と無効時の動作を定義し、列挙された例を網羅する必要があります。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
typescript
領域
compilers
issue の種類
機能追加
難易度
5/5
見積もり時間
1週間以上
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
30/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。