microsoft / microsoft/TypeScript

Type is not checked when using object spread and computed property of type `number`

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

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

Bug Domain: check: Excess Property Checking
主要言語
Go
スター
111k
フォーク
14.4k
平均マージ
1日 19時間
マージ済み PR(30日)
117

説明

Bug Report

🔎 Search Terms

spread computed property number reduce

🕗 Version & Regression Information
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about _________
⏯ Playground Link

Playground link with relevant code

💻 Code
declare const id: number;

type Acc = { [key: string]: number };
declare const acc: Acc;

// Expected error, but got none. ❌
const acc2: Acc = {
    ...acc,
    [id]: 'invalid',
};

If we change the type of the computed property id from number to string, it works as expected (we get an error).

declare const id: string;

type Acc = { [key: string]: number };
declare const acc: Acc;

// Expected error, and got none. ✅
const acc2: Acc = {
    ...acc,
    [id]: 'invalid',
};

Alternatively, if we remove the spread of acc it works as expected:

declare const id: number;

type Acc = { [key: string]: number };
// declare const acc: Acc;

// Expected error, and got none. ✅
const acc2: Acc = {
    [id]: 'invalid',
};

My workaround for now is to convert the type before it's used as a computed property:

declare const id: number;

type Acc = { [key: string]: number };
declare const acc: Acc;

// Expected error, and got none. ✅
const acc2: Acc = {
    ...acc,
    [id.toString()]: 'invalid',
};
🙁 Actual behavior

See code comments above.

🙂 Expected behavior

See code comments above.

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

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

はじめの一歩

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

調査の方向性

リンクされた TypeScript Playground の再現コードから始め、数値の computed property と string の computed property を、object spread がある場合とない場合で比較します。オブジェクトリテラルの type-checking の動作を追跡し、数値プロパティの場合はエラーが報告される一方で、他の例は期待どおりの動作を維持することを確認します。

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

評価

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

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

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