microsoft / microsoft/TypeScript
Allow narrowing generic type to single element of union type
まだ誰も着手していません。
- 主要言語
- Go
- スター
- 111k
- フォーク
- 14.3k
- 平均マージ
- 2日 4時間
- マージ済み PR(30日)
- 132
説明
It is a fairly common pattern to create a set of generic components, and then to use the generic component type within the system. Currently, typescript has an issue handling cases where some values of a generic component depend on others.
TypeScript Version: 2.4.0 / nightly (2.5.0-dev.201xxxxx)
2.4
Code
let index = {
a: {
arg: 'a',
fn: (a: string) => {a.toUpperCase()}
},
b: {
arg: 1,
fn: (b: number) => {b + 1}
}
}
let runFn= <K extends keyof (typeof index)>(key: K) => {
let i = index[key]
let arg = i.arg
let fn = i.fn
fn(arg) // Error: compiler doesn't understand that fn and arg will match
}
Expected behavior:
No error, since we know that arg and fn are pulled off of the same map entry.
Actual behavior:
Error: cannot invoke expression whose type lacks a call signature
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
まず、提供された TypeScript 2.4 の例を再現し、fn(arg) を呼び出したときのエラーを確認します。次に、ジェネリックキーと依存する union メンバーを介したインデックスアクセスについて、型チェックの経路を追跡します。完了とは、型安全性を弱めることなく例がコンパイルできる状態です。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- typescript
- 領域
- compilers
- issue の種類
- バグ
- 難易度
- 5/5
- 見積もり時間
- 1週間以上
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 30/100