microsoft / microsoft/TypeScript

`unknown` isn’t narrowed by assignment

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

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

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

説明

Bug Report

🔎 Search Terms

try block

🕗 Version & Regression Information
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about try-catch blocks
⏯ Playground Link

Playground link with relevant code

💻 Code
function f(request: {params: {[param: string]: unknown;}}) {
    let x = request.params.x;
    try {
        x = validate(x);
    } catch (e) {}

    try {
        takesString(x);  // Fails to compile, x is of type unknown.
    } catch (e) {}
}

function validate(x: unknown): string { return ""; }
function takesString(s: string) {}
🙁 Actual behavior

In this example, the code fails to compile because tsc has deduced x in the second block to be of type unknown

🙂 Expected behavior

Since we have successfully exited the first try-catch block, x's type should be narrowed to string in the second.

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

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

はじめの一歩

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

調査の方向性

まずリンクされている TypeScript Playground の例を実行して、2 つの try-catch ブロック周辺で発生する失敗を再現します。次に、try-catch の境界をまたぐ代入に対するコンパイラーの control-flow narrowing を追跡し、示されているケースの回帰テストを追加します。最初のブロックの後で tsc が takesString(x) を受け入れ、他の箇所の narrowing を弱めていなければ完了です。

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

評価

技術スタック
typescript
領域
compilers
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
明確に書かれている
初心者へのやさしさ
35/100

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

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