microsoft / microsoft/TypeScript

Improve error message for incompatible signatures in union type from typed/untyped function call

Ouverte
#27,422 6 commentaires 1 réaction 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

Domain: Error Messages Experience Enhancement Suggestion
Langage dominant
Go
Étoiles
111k
Forks
14.4k
Merge moyen
1 j 19 h
PR mergées (30 j)
117

Description

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)

Guide de contribution

Ouvrir le guide de contribution

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Piste de recherche

Commencez par exécuter l’exemple TypeScript de l’issue et recherchez l’erreur 2349 dans les diagnostics du compilateur. Suivez la manière dont les signatures d’appel de l’union produisent le message actuel. Le travail est terminé lorsque les appels de fonction typés et non typés incompatibles reçoivent un diagnostic explicatif plus clair, tout en continuant à couvrir les exemples existants.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Stack technique
typescript
Domaine
compilers
Type d'issue
Fonctionnalité
Difficulté
4/5
Temps estimé
3-5 jours
Activité
À l'abandon
Clarté
Plutôt claire
Accessibilité débutants
35/100

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.