microsoft / microsoft/TypeScript
Type Merging between extends and intersection
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.3k
- Merge medio
- 2g 4h
- PR unite (30g)
- 132
Descrizione
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.
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia con la riproduzione di TypeScript 1.8.10 nell'issue e confronta il modo in cui FooBarA estende Foo e Bar con l'intersezione equivalente Foo & Bar. Il lavoro è completo quando gli overload compatibili vengono uniti per FooBarA, mentre gli overload specifici di string-literal realmente in conflitto continuano a produrre un errore.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- typescript
- Ambito
- compilers
- Tipo di issue
- Funzionalità
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 25/100