microsoft / microsoft/TypeScript
Type Merging between extends and intersection
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- Go
- Estrellas
- 111k
- Forks
- 14.3k
- Merge medio
- 1 d 19 h
- PR fusionados (30 d)
- 117
Descripción
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.
Guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Línea de trabajo
Empieza con la reproducción de TypeScript 1.8.10 del issue y compara cómo FooBarA extiende Foo y Bar con la intersección equivalente Foo & Bar. Se considera terminado cuando las sobrecargas compatibles se combinan para FooBarA, mientras que las sobrecargas específicas de literales de cadena que realmente entran en conflicto siguen produciendo un error.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- typescript
- Área
- compilers
- Tipo de issue
- Nueva funcionalidad
- Dificultad
- 5/5
- Tiempo estimado
- Más de una semana
- Estado de actividad
- Estancado
- Claridad
- Bastante claro
- Aptitud para principiantes
- 25/100