microsoft / microsoft/TypeScript

Generics return type of function reported not assignable.

Ouverte
#43,176 2 commentaires 0 réactions 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

Bug Domain: check: Type Inference
Langage dominant
Go
Étoiles
111k
Forks
14.3k
Merge moyen
2 j 4 h
PR mergées (30 j)
132

Description

Bug Report

🔎 Search Terms

return value, typescript, generics

🕗 Version & Regression Information

This changed between versions 3.5.1 and 3.6.3

⏯ Playground Link

https://www.typescriptlang.org/play?ts=3.6.3#code/C4TwDgpgBAKuEGECGBnCBBAPOgfFAvFAN5QAmSwSAXFOlAL4DcAUKJLPMmgEKbcFQUwAE4BLAHYBzPIRLlKAfhrdGUAMYB7UhBpCxU1U1bwOkLhl7oANFH6E9E6QLhnUGbHgA+pxG97ccFmY1ABtUFCgAZQhhADdRNWgiZihUqEkIYH8BBykcAApNbV0RRxt5JCVbAEoaF18ePjxktNb1DXEhKGEIFABXEOA6zj8mgTkKJBsi6CM2tJ7gPuFxbt6B4BZW+mYUtIzgLFx8ipp0Wp9zI+a9+cXl1YnKBi20nduoADM+8TUARnyF3qV14JFEpBo4j6AFsAEYxBg3eape4rKDAAAWohQADoDtx8gByP4AJgAzH9CdVXql3q1vr8SYDhq40OhQVBwZCYfDhIjiB9WppOsA1sBYkgQgJMdi8ZkCcTyZTqYKFpkHmKJSEaQxdvSfmoyczLm52ZgwRCoFC4Qj6DZck4WsjUasZbj8UTSRTCTZCQAWACsADYqTqdvQgA

💻 Code
type TypeCaseA<A> = { data: A };
type TypeCaseB<B = string> = { data?: B; code: string; };
type TypeCaseAB<A, B = string> = TypeCaseA<A> | TypeCaseB<B>;

class Service {
    getB<B = string>(code: string, data?: B): TypeCaseB<B> {
        const result: TypeCaseB<B> = { data, code };
        return result;
    }

    getA<A>(data: A): TypeCaseA<A> {
        return { data };
    }

    func1(): TypeCaseAB<{ id: number }> {
        /* The following line is blamed with:
          Type 'TypeCaseB<string | { id: number; }>' is not assignable to type 'TypeCaseAB<{ id: number; }, string>'.
            Type 'TypeCaseB<string | { id: number; }>' is not assignable to type 'TypeCaseB<string>'.
              Type 'string | { id: number; }' is not assignable to type 'string'.
                Type '{ id: number; }' is not assignable to type 'string'.
         */
        return this.getB('1231');
    }

    func2(): TypeCaseAB<{ id: number }> {
        const retval = this.getB('1231');
        return retval;
    }

    func3(): TypeCaseAB<{ id: number }, string> {
        return this.getB('1231', '456');
    }
}
🙁 Actual behavior

func1 is tested with the following error:

          Type 'TypeCaseB<string | { id: number; }>' is not assignable to type 'TypeCaseAB<{ id: number; }, string>'.
            Type 'TypeCaseB<string | { id: number; }>' is not assignable to type 'TypeCaseB<string>'.
              Type 'string | { id: number; }' is not assignable to type 'string'.
                Type '{ id: number; }' is not assignable to type 'string'.

There should be no difference from func2 which is tested correct.

🙂 Expected behavior

No error reported.

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

Reproduire le diagnostic dans le TypeScript Playground lié en utilisant les exemples TypeCaseA, TypeCaseB, TypeCaseAB et Service.func1/func2/func3. Comparer la gestion du type de retour générique dans func1 avec les cas acceptés func2 et func3. Le travail est terminé lorsque func1 ne produit aucune erreur tout en conservant le type de retour TypeCaseAB attendu.

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

Évaluation

Stack technique
typescript
Domaine
compilers
Type d'issue
Bug
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.