microsoft / microsoft/TypeScript
Improve error message for incompatible signatures in union type from typed/untyped function call
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- Go
- Estrellas
- 111k
- Forks
- 14.3k
- Merge medio
- 2 d 4 h
- PR fusionados (30 d)
- 132
Descripción
Search Terms
untyped function call error message union type incompatible signature
Example
This code errors as follows:
function foo<T>(x: T | (() => string)) {
if (typeof x === "function") {
let a = x();
// ^ Cannot invoke an expression whose type lacks a call signature.
//Type '(() => string) | (T & Function)' has no compatible call signatures. [2349]
}
}
which feels strange given that both of the following are fine:
function fooL<T>(x: T) {
if (typeof x === "function") {
let a = x();
}
}
function fooR(x: () => string) {
if (typeof x === "function") {
let a = x();
}
}
If an evaluation contexts accepts values of type A and values of type B, then it should accepts values of type A | B. I think what is going on is that the call signature from T & Function is untyped, which is incompatible with the typed signature of () => string.
I don't think this is very obvious to a user, and they end up seeing an application that works for both branches of a union, but not their composition.
Suggestion
I think making the example work is out of scope. My suggestion is to improve the error message, something like:
function foo<T>(x: T | (() => string)) {
if (typeof x === "function") {
let a = x();
// ^ Cannot invoke an expression whose type lacks a call signature.
// Type '(() => string) | (T & Function)' has no compatible call signatures. [2349]
// Cannot combine untyped function call with typed function call
}
}
Checklist
My suggestion meets these guidelines:
- This wouldn't be a breaking change in existing TypeScript / JavaScript code
- This wouldn't change the runtime behavior of existing JavaScript code
- This could be implemented without emitting different JS based on the types of the expressions
- This isn't a runtime feature (e.g. new expression-level syntax)
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 ejecutando el ejemplo de TypeScript del issue y busca el error 2349 en los diagnósticos del compilador. Traza cómo las firmas de llamada de la unión producen el mensaje actual. Se considera terminado cuando las llamadas a funciones tipadas y no tipadas incompatibles reciben un diagnóstico explicativo más claro y los ejemplos existentes siguen estando cubiertos.
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
- 4/5
- Tiempo estimado
- 3-5 días
- Estado de actividad
- Estancado
- Claridad
- Bastante claro
- Aptitud para principiantes
- 35/100