microsoft / microsoft/TypeScript

Type narrowing on object properties lost in async IIFE

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

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

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

説明

TypeScript Version: 3.4.0-dev.20190327

Search Terms:
narrowing iife control flow

Code

declare const x: string | undefined;
declare const y: { z: string | undefined };
function needsString(it: string) { return it; }

function a() {
  if (!x) {
    throw new Error("Missing x");
  }
  const res1 = (() => needsString(x))(); // all good, per #8849
  const res2 = (async () => needsString(x))();  // ditto

  if (!y.z) {
    throw new Error("Missing z.")
  }

  const res3 = (() => needsString(y.z))();  // still good
  const res4 = (async () => needsString(y.z))();  // now things blow up  
}

Expected behavior:
The call to needsString in the expression for res4 succeeds, like the one in res2 and res3. Even though the function is async, the code in it -- at least that's before an await -- runs immediately, I believe, and so should be subject to the narrowing (esp. given that res2 works).

Actual behavior:
Error for needsString(y.z): Argument of type 'string | undefined' is not assignable to parameter of type 'string'.

Playground Link: https://www.typescriptlang.org/play/index.html#src=declare%20const%20x%3A%20string%20%7C%20undefined%3B%0Adeclare%20const%20y%3A%20%7B%20z%3A%20string%20%7C%20undefined%20%7D%3B%0Afunction%20needsString(it%3A%20string)%20%7B%20return%20it%3B%20%7D%0A%0Afunction%20a()%20%7B%0A%20%20if%20(!x)%20%7B%0A%20%20%20%20throw%20new%20Error(%22Missing%20x%22)%3B%0A%20%20%7D%0A%20%20const%20res1%20%3D%20(()%20%3D%3E%20needsString(x))()%3B%20%2F%2F%20all%20good%2C%20per%20%238849%0A%20%20const%20res2%20%3D%20(async%20()%20%3D%3E%20needsString(x))()%3B%20%20%2F%2F%20ditto%0A%0A%20%20if%20(!y.z)%20%7B%0A%20%20%20%20throw%20new%20Error(%22Missing%20z.%22)%0A%20%20%7D%0A%0A%20%20const%20res3%20%3D%20(()%20%3D%3E%20needsString(y.z))()%3B%20%20%2F%2F%20still%20good%0A%20%20const%20res4%20%3D%20(async%20()%20%3D%3E%20needsString(y.z))()%3B%20%20%2F%2F%20now%20things%20blow%20up%20%20%0A%7D

Related Issues:
https://github.com/Microsoft/TypeScript/pull/8849, which I think was supposed to fix IIFE issues like this, but appears to have missed a case (or I'm missing something).

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

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

はじめの一歩

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

調査の方向性

報告されたバージョン 3.4.0-dev を使って、リンクされた TypeScript Playground で issue の例を再現し、async IIFE 周辺の x と y.z の narrowing ケースを比較します。オブジェクトプロパティに対するコンパイラーの制御フロー narrowing を追跡し、既存の res2 と res3 の動作を変更せずに res4 の呼び出しで y.z が string として受け入れられるよう、カバレッジを追加します。

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

評価

技術スタック
typescript
領域
compilers
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
45/100

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

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