microsoft / microsoft/TypeScript

Assigning array values by index treats Array<A> | Array<B> as Array<A | B>

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

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

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

説明

TypeScript Version: 4.0.0-dev.201xxxxx
reproducible starting from at least 3.3.3

Search Terms: discriminated unions, distributive

Code

// A *self-contained* demonstration of the problem follows...
// Test this by running `tsc` on the command-line, rather than through another build tool such as Gulp, Webpack, etc.

function main() {
    var a: (number[] | string[])  = []; 
    a[0] = 3;
    a[1] = "xxx";
}

Expected behavior:
error TS2322: Type '"xxx"' is not assignable to type 'number'.

Actual behavior:
All is compiled without warnings or errors.
Keep in mind that this works as expected:

var a: (number[] | string[])  = [3, "xxx"]; // Not OK as it should be 

Thus, depending on how exactly we're initialising the array, behaviour differs.
Playground Link:
https://www.typescriptlang.org/play?ts=4.0.0-dev.20200508#code/C4TwDgpgBAggTnAcgVwLYGVhygXigOzQCMI4BtAXSgB8oBnLAS3wHNKBuAKE4DNl8AxsEYB7fFFQBDZgAoAlFADenKKqgA3SdkkAuKDMKoS5KrQZxmbCgtxQOUFWslkADFTwBmLmqjOAjO5QAEQAHmFBXAC+nEA

Related Issues:

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

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

はじめの一歩

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

調査の方向性

自己完結した TypeScript の再現コードから始め、コマンドラインの tsc で実行し、インデックス付き代入と配列リテラルによる初期化を比較します。issue にはソースファイルもテストも指定されていないため、配列型の union に対するインデックス付き代入の checker パスを追跡します。再現した代入が期待される型エラーを報告し、関連する初期化の動作を後退させないことを完了条件とします。

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

評価

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

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

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