microsoft / microsoft/TypeScript
Improve error message for incompatible signatures in union type from typed/untyped function call
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.3k
- Merge medio
- 2g 4h
- PR unite (30g)
- 132
Descrizione
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)
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia eseguendo l’esempio TypeScript nell’issue e cerca l’errore 2349 nei diagnostici del compilatore. Ricostruisci come le firme di chiamata dell’unione producono il messaggio attuale. Il lavoro è completato quando le chiamate a funzioni tipizzate e non tipizzate incompatibili ricevono un diagnostico esplicativo più chiaro e gli esempi esistenti continuano a essere coperti.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- typescript
- Ambito
- compilers
- Tipo di issue
- Funzionalità
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 35/100