microsoft / microsoft/TypeScript
Type Merging between extends and intersection
まだ誰も着手していません。
- 主要言語
- Go
- スター
- 111k
- フォーク
- 14.4k
- 平均マージ
- 1日 19時間
- マージ済み PR(30日)
- 117
説明
TypeScript Version:
1.8.10
Code
interface Foo {
on(type: 'foo', listener: (event: MouseEvent) => boolean): boolean;
on(type: string, listener: (event: Event) => boolean): boolean;
}
interface Bar {
on(type: 'bar', listener: (event: MSGestureEvent) => boolean): boolean;
on(type: string, listener: (event: Event) => boolean): boolean;
}
interface FooBarA extends Foo, Bar {} // Error: not correctly extended
interface FooBarB extends Foo, Bar {
on(type: 'foo', listener: (event: MouseEvent) => boolean): boolean;
on(type: 'bar', listener: (event: MSGestureEvent) => boolean): boolean;
on(type: string, listener: (event: Event) => boolean): boolean;
}
type FooBarC = Foo & Bar;
let foobar: FooBarC;
foobar.on('foo', (event) => { return false; });
Expected behavior:
That FooBarA would work instead of throwing an error.
Actual behavior:
FooBarA complains Interface 'FooBarA' cannot simultaneously extend types 'Foo' and 'Bar'. Named property 'on' of types 'Foo' and 'Bar' are not identical.
While that is true in the strictest sense, the "base" override matches, which means in theory the string literal types could be merged and the error should only occur if there is a conflict between a specific string literal type.
Using the intersection type works perfectly fine and the resulting type mirrors the runtime behaviour. Using the "reimplement all methods" (FooBarB) gets really tedious really quickly when you are trying to do something like model event listeners like the above.
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
issue にある TypeScript 1.8.10 の再現から始め、FooBarA が Foo と Bar を拡張する場合と、対応する Foo & Bar の intersection とを比較します。FooBarA では互換性のある overload がマージされ、一方で実際に競合する具体的な string-literal overload は引き続きエラーになることが完了条件です。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- typescript
- 領域
- compilers
- issue の種類
- 機能追加
- 難易度
- 5/5
- 見積もり時間
- 1週間以上
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 25/100