microsoft / microsoft/TypeScript

negating type constraints

オープン
#7,993 コメント 14 件 リアクション 24 件 担当者 0 名 GitHub で見る

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

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

説明

Sometimes it's useful to put a limit on what a type parameter can be. In a way it is a counterpart of the extends constraint.

Problem

Consider an example, a classic function that takes whatever and returns void:

function ignore<a>(value: a) : void {};

However we must not apply this function to Promises, because it might get us a temporal leak if we do.

function readFileAsync(): Promise<string>;
ignore(readFileAsync()); // <-- untracked promise, temporal leak

Unfortunately it is way too easy to get into a situation when a promise is passed to that function unintentionally as a result of refactoring:

// before
function readFileSync(): string;
ignore(readFileSync()); // typechecks, works as intended, no problem

// after refactoring
function readFileAsync(): Promise<string>; // <-- went async here
ignore(readFileAsync()); // typechecks, unintended temporal leak, big problem
Solution

The situation above could have been avoided if TypeScript allowed negating constraints:

function ignore<a unlike Promise<any>>(value: a): void {} // <-- hypothetical syntax

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

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

はじめの一歩

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

調査の方向性

提案されている unlike 構文と、issue における既存の extends 制約との比較から始めます。TypeScript のジェネリック制約と型チェックの動作を確認し、示されているリファクタリングのケースで Promise<any> の除外がどのように機能すべきかを定義します。ファイルやテストのエントリーポイントが指定されていないため、合意された設計に加えて実装とテストが揃っていることが完了の条件です。

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

評価

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

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

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