microsoft / microsoft/TypeScript

Improve Type guards to correctly work with for-of loops

オープン
#29,283 コメント 2 件 リアクション 0 件 担当者 0 名 GitHub で見る

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

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

説明

Search Terms

for-of, Type Guard

Suggestion

At the moment a paradigm of call-Parameter-Testing I apply works well in case of one parameter, but if I want to shorten the testing of multiple parameters using a for-of loop the type assertion fails.

Use Cases

Correct type guarding of multiple parameters which are supposed to be the same type but to be handled differently

Examples

type generalObj<T> = {
    value: T,
};

type numObj = generalObj<number>

const isNumObj = (testObj: generalObj<any>): testObj is numObj => {
    //some logic here...
    return true;
}

const numConsumer = (param: numObj) => param;

const shouldWork = (shouldBeNumObj: generalObj<number | string>) => {
    for (const c of [shouldBeNumObj]) {
        if (!isNumObj(c)) {
            return console.error('no numObj')
        }
    }
    // error here despite the right type ensured
    numConsumer(shouldBeNumObj);
}

const doesWork = (shouldBeNumObj: generalObj<number | string>) => {
    if (!isNumObj(shouldBeNumObj)) {
        return console.error('no numObj')
    }
    numConsumer(shouldBeNumObj);
}

const shouldWork2 = (shouldBeNumObj: Array<generalObj<number | string>>) => {
    for (const c of shouldBeNumObj) {
        if (!isNumObj(c)) {
            return console.error('no numObj')
        }
    }
    numConsumer(shouldBeNumObj[0]);
}

const baseTypeNumConsumer = (param: number) => param; 
// curtesy of @nmain
const shouldWork3 = (shouldBeNum: string | number) => {
    for (const v of [shouldBeNum]) {
        if (typeof v !== "number") {
            return;
        }
    }
    baseTypeNumConsumer(shouldBeNum); // compiler error, but should be valid
}

const stringConsumer = (param: generalObj<string>) => param;

const couldBreakCode = (anyObjS: Array<generalObj<any>>) => {
    for (const c of anyObjS) {
        if (!isNumObj(c)) {
            return console.error('no numObj')
        }
    }
    stringConsumer(anyObjS[0])
}

const becauseThisFails = (anyObj: generalObj<any>) => {
    if (!isNumObj(anyObj)) {
        return console.error('no numObj')
    }
    stringConsumer(anyObj);
}

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
    • Could break compilation of already error-prone code where Type testing was disabled with any
  • 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 の TypeScript の例を再現し、for-of ループを通じてテストされる変数を control-flow narrowing がどのように扱うかを追跡してください。完了条件は、有効な narrowing のケースがコンパイルされ、示されている any ベースのケースが unsound にならないことです。

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

評価

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

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

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