microsoft / microsoft/TypeScript

new Map() infers type only from the first item in the iterable

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

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

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

説明

TypeScript Version: 3.8.3

Search Terms: map infer type first item

Code

type Lookup = Map<number, boolean | string>;

const foo = new Map([
    [1, true],
    [2, 'foo']
]);

const bar: Lookup = new Map([
    [1, true],
    [2, 'foo']
]);

const baz: Lookup = new Map<number, boolean | string>([
    [1, true],
    [2, 'foo']
]);

Expected behavior: All 3 assignments work.

Actual behavior: The first 2 assignments fail because the type of the map is inferred as Map<number, () => boolean> (because of the first item). The 3rd one works but duplicates the type of the map.

Playground Link

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

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

はじめの一歩

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

調査の方向性

まず、3つの Map の例を使って、リンク先の Playground で TypeScript 3.8.3 の動作を再現します。checker が Map コンストラクターに渡される iterable の型をどのように推論するかを追跡し、その後、3つすべての代入で、重複した明示的な型を指定しなくても boolean 値と string 値の混在が受け入れられることを確認します。

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

評価

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

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

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