microsoft / microsoft/TypeScript
Callback argument type not inferred for union of interfaces
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- Go
- Estrellas
- 111k
- Forks
- 14.4k
- Merge medio
- 1 d 19 h
- PR fusionados (30 d)
- 117
Descripción
🔎 Search Terms
"implicitly has an" inference
🕗 Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about "inference"
⏯ Playground Link
💻 Code
export interface AsyncResultCallback<T, E = Error> {
(err?: E | null, result?: T): void;
}
export interface AsyncResultIterator<T, R, E = Error> {
(item: T, callback: AsyncResultCallback<R, E>): void;
}
export interface AsyncResultIteratorPromise<T, R> {
(item: T): Promise<R>;
}
declare function mapLimit<T, R, E = Error>(
arr: T[],
limit: number,
iterator: AsyncResultIteratorPromise<T, R> | AsyncResultIterator<T, R, E>,
): Promise<R[]>;
mapLimit([1,2,3], 3, async (n) => {
return n ** 2;
});
🙁 Actual behavior
The type of n in the mapLimit callback is inferred as any:
Parameter 'n' implicitly has an 'any' type. (7006)
🙂 Expected behavior
I would expect the type of n to be inferred as number.
Additional information about the issue
This example is based on the types for the async package:
Note that if the type for iterator in mapLimit is changed to either just AsyncResultIteratorPromise<T, R> or just AsyncResultIterator<T, R, E> (that is, no longer a union type), things work as expected and n has type number.
I found a very old discussion of what looks to be the same issue in the DefinitelyTyped repo: https://github.com/DefinitelyTyped/DefinitelyTyped/issues/24297. The issue described there is exactly the one I'm facing. One commenter claimed that things worked correctly, and another commenter claimed that changing the way that async was imported had some effect? Neither of those sounds plausible to me.
I found some issues that may describe the same or a similar problem:
- https://github.com/microsoft/TypeScript/issues/56881
- https://github.com/microsoft/TypeScript/issues/55632
- https://github.com/microsoft/TypeScript/issues/52047
- https://github.com/microsoft/TypeScript/issues/47599
However, my knowledge of TypeScript internals/terminology isn't strong enough to say for sure that this isn't a novel issue I'm facing. If this is in fact a duplicate, please close it as such and I'll add this example to the relevant issue. Thank you!
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 el Playground enlazado y el ejemplo mínimo de mapLimit; después compara la inferencia cuando iterator es cada interfaz por separado frente a su unión. Revisa las incidencias relacionadas de TypeScript y la definición de tipos async enlazada para obtener el contexto correspondiente. Se considera terminado cuando, en el caso de la unión, n se infiere como number en el parámetro del callback sin un error de implicit-any.
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