microsoft / microsoft/TypeScript

Interface merging for any property names does not allow type narrowing

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

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

Awaiting More Feedback Suggestion
主要言語
Go
スター
111k
フォーク
14.3k
平均マージ
2日 4時間
マージ済み PR(30日)
132

説明

TypeScript Version: 4.1.0-dev.20201002

Search Terms: interface merging merge object any name subsequent property declarations

Code

interface PropsContainer {
  props: {
    [propName: string]: any;
  }
}

interface PropsContainer {
  props: {
    [propName: string]: any;
    specialProp: string;
  }
}

Expected behavior:
This should be allowed. The merged interface should result in a type with props that have any prop name as any except for specialProp which is a string.

This would be useful in a case where a third party library (such as @types/react-test-renderer) declare an object type with { [propName: string]: any } syntax, but you want to narrow the value of the property for specific types via interface merging.

Note that even { specialProp?: any, [propName: string]: any } is not allowed even though the types are functionally identical in this case.

Actual behavior:
This results in a Subsequent property declarations must have the same type.

Playground Link: https://www.typescriptlang.org/play?#code/JYOwLgpgTgZghgYwgAgApQPYAcDOBhDcOUaZAbwChlktNcAucq65AbVuwDk4BbCRnGCigA5gF1GcEAE8A3MwC+FJRVCRYiFOmz5CYYiFKVqHBkxbIcWCAmBwANtqwB+STPkX2dbnwFDREshScorKQA

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

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

はじめの一歩

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

調査の方向性

リンクされた TypeScript Playground の再現コードから始め、マージされた 2 つの PropsContainer 宣言について診断を確認します。次に、後続のプロパティ宣言とインデックスシグネチャに対する型チェックの経路を追跡します。示されたマージが受け入れられ、specialProp が string に絞り込まれ、既存の互換性のない再宣言が引き続き拒否されれば完了です。

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

評価

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

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

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