microsoft / microsoft/TypeScript
Improve error message for incompatible signatures in union type from typed/untyped function call
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Go
- Sterne
- 111k
- Forks
- 14.3k
- Ø Merge
- 1 T. 19 Std.
- Gemergte PRs (30 T.)
- 117
Beschreibung
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)
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginne damit, das TypeScript-Beispiel im Issue auszuführen, und durchsuche die Compilerdiagnosen nach Fehler 2349. Verfolge, wie die Aufrufsignaturen der Union die aktuelle Meldung erzeugen. Erledigt ist die Aufgabe, wenn die inkompatiblen typisierten und untypisierten Funktionsaufrufe eine klarere erklärende Diagnose erhalten und die vorhandenen Beispiele weiterhin abgedeckt sind.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- typescript
- Bereich
- compilers
- Issue-Typ
- Feature
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 35/100