Only number index type is inferred from `any` computed property key

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

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

評価

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

調査の方向性

リンクされている TypeScript Playground の再現コードから始め、any キーに対する計算プロパティ/インデクサーの推論を追跡します。意図された推論後のインデクサー型を特定し、その結果を、示されている文字列アクセスの例と照合して検証します。そのうえで、関連する型チェックテストにカバレッジを追加または更新します。

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

説明

Bug Domain: Index Types

Bug Report

🔎 Search Terms
  • Computed properties
  • Indexers
  • Index signatures
🕗 Version & Regression Information
  • This is the behavior in every version I tried.
⏯ Playground Link

Playground link with relevant code

💻 Code
declare let a: any;

const obj = {
  [a]: "s",
}; // { [x: number]: string }

obj["abc"]; // error
🙁 Actual behavior

A numeric indexer is inferred. String index access marked as invalid.

🙂 Expected behavior

A string indexer is expected. No error for string access.

declare let a: any;

const obj = {
  [a]: "s",
}; // { [x: number]: string }

obj["abc"]; // no error

I created a small PR that addresses the issue as an example but I'm still not sure if the current behavior is intentional or not.

EDIT: I'm still not sure if the inferred indexer should have a String type, perhaps a union of the allowed primitive types fits better, e.g.:

declare let a: any;

const obj = {
  [a]: "s",
}; // { [x: string | number | symbol]: string }
主要言語
Go
スター
111k
フォーク
14.4k
平均マージ
1日 19時間
マージ済み PR(30日)
117

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

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

はじめの一歩

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

microsoft/TypeScript のほかの issue

microsoft/TypeScript の issue をすべて見る

似ている issue

Go の issue をもっと見る

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

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