microsoft / microsoft/TypeScript

Broken inference between index typed object and Record

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

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

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

説明

TypeScript Version: 2.2.2

Code

interface Errors {
  [field: string]: {
    key: string
    message: string
  }
}

const errors: Errors = {}

function genericObjectFunction<K extends string, V>(obj: Record<K, V>): [K, V][] {
  return []
}

/*
 Argument of type 'Errors' is not assignable to parameter of type 'Record<string, never>'.
 Index signatures are incompatible.
 Type '{ key: string; message: string; }' is not assignable to type 'never'.
 */
genericObjectFunction(errors)

The types seem generally compatible though, as expected this is legal:

const errorRecord: Record<string, { key: string, message: string }> = errors

Expected behavior:
genericObjectFunction(errors) should compile.

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

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

はじめの一歩

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

調査の方向性

Issue にある最小限の TypeScript 2.2.2 再現コードから始め、genericObjectFunction と、Errors のインデックスシグネチャから Record への推論に焦点を当ててください。完了条件は、示されている Record 互換性を維持したまま genericObjectFunction(errors) がコンパイルできることです。

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

評価

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

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

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