microsoft / microsoft/TypeScript

Additive inverse of number literals

オープン
#30,914 コメント 1 件 リアクション 5 件 担当者 0 名 GitHub で見る

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

Awaiting More Feedback Suggestion
主要言語
Go
スター
111k
フォーク
14.3k
平均マージ
2日 4時間
マージ済み PR(30日)
132

説明

Search Terms

Using unary minus on variable that is of number-literal type. Negative number literals.

Suggestion

This is a specific case of #26382. My proposal is simply that if we apply unary minus on an expression whose type is a union of number literal types, the result should be a union of their additive inverses:

declare var a: 1 | 2 | 3;
const b = -a; // should be -1 | -2 | -3

Furthermore we could add a unary minus type operator:

-(42) === -42
-(A | B) === -A | -B
-number === number
-(not number) // error

Both should be very easy and uncontroversial to implement.

Use Cases

function compare(a: C, b: C): -1 | 0 | 1;

function reverseCompare(a: C, b: C): -1 | 0 | 1
{
  return -compare(a, b); // shouldn't throw an error
}

The type operator would be just for sake of completeness

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, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

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

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

はじめの一歩

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

調査の方向性

まず issue #26382 の関連提案と、数値リテラルのユニオンに対する単項マイナスの型チェック動作を確認します。完了条件は、例から -1 | -2 | -3 が推論され、reverseCompare が受け入れられる一方で、無効な非数値オペランドは引き続きエラーになることです。issue ではソースファイルやテストは指定されていません。

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

評価

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

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

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