microsoft / microsoft/TypeScript

Type discrimination in function calls for callable types doesn't work if discriminating property is optional

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

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

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

説明

TypeScript Version: 3.7.0-dev.20191015

Search Terms: type discrimination, callable, signatures, implicit any

Code

function test(
  a:
    | { type?: 1; property: (a: number) => void }
    | { type: 2; property: (a: string) => void },
) {
  return a.property
}


test({
  property: (x) => { // complaining that x implicitly has type any
    console.log('hi')
  },
})

Expected behavior:

x should be inferred to be number.

Actual behavior:

x is any.

If you change type?: 1 to type: 1, it works as expected.

Playground Link: link

Related Issues: #7294 (closed with #29011 - see second half of the original post, I believe it describes why this issue occurs)

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

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

はじめの一歩

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

調査の方向性

まず、提供されている TypeScript 3.7 の再現コードを実行し、必要な discriminant バリアントと動作を比較します。callable-type discrimination の背景については、関連する issue #7294 と、そこから参照されている #29011 を確認してください。optional な discriminating property が、callback parameter を any ではなく number として推論し続ければ完了です。

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

評価

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

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

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