microsoft / microsoft/TypeScript
Differing results for inference produced when identical overload count changes
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
I happened to notice this while looking at something else inference related a bit ago. We already have a test that is looking for checking this, fixingTypeParametersRepeatedly3.ts, added in #2356, but it was accepted in #16368 (strict generic checks) with a (tiny, unnoticeable in all the other changes) baseline showing the fault.
Code
interface Base {
baseProp;
}
interface Derived extends Base {
toBase?(): Base;
}
var derived: Derived;
declare function foo<T>(x: T, func: (p: T) => T): T;
var result = foo(derived, d => d.toBase());
// bar should type check just like foo.
// result2 should have the same type as result
declare function bar<T>(x: T, func: (p: T) => T): T;
declare function bar<T>(x: T, func: (p: T) => T): T;
var result2 = bar(derived, d => d.toBase());
Expected behavior:
result and result2 are assigned the same type (there's a comment in the test asserting as much) - they are implemented identically except bar has an extra identical overload. (note: this can happen easily in the real world nowadays via intersection types! Intersecting multiple interfaces with similar base interfaces can cause exactly this situation.)
Actual behavior:
result typechecks as Derived
result2 typechecks as Base (seems wrong, since Base definitely doesn't have the toBase method used in the callback!)
Derived and Base are assignable to one another here, since toBase is optional, which is why the inference can succeed at all; however, Base, as reported in the second case can't actually have been the type used to typecheck the lambda body, as the toBase call doesn't cause an error in the test - I think the likely reason for the change in behavior is this removal, based on the content of the removed comment.
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
Comienza con la prueba existente fixingTypeParametersRepeatedly3.ts y su aserción de que result y result2 tienen el mismo tipo. Reproduce la inferencia diferente para las declaraciones con una y dos sobrecargas, y luego sigue el comportamiento de inferencia asociado con la eliminación descrita en el PR enlazado de TypeScript; el trabajo estará terminado cuando ambos resultados pasen la comprobación de tipos de forma consistente sin debilitar la comprobación del callback.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- typescript
- Área
- compilers
- Tipo de issue
- Error
- Dificultad
- 4/5
- Tiempo estimado
- 3-5 días
- Estado de actividad
- Estancado
- Claridad
- Bastante claro
- Aptitud para principiantes
- 35/100