microsoft / microsoft/TypeScript

Parameters<T> and ReturnType<T> limitation

Ouverte
#38,685 2 commentaires 9 réactions 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

Awaiting More Feedback Suggestion
Langage dominant
Go
Étoiles
111k
Forks
14.4k
Merge moyen
1 j 19 h
PR mergées (30 j)
117

Description

TypeScript Version: 3.9.2

Search Terms:
Generic Method Extend Parameters ReturnType

Code

export class Test {

    myMethod(): string {
        return "42";
    }

    myMethod2(a: string): string {
        return "42" + a;
    }

    //  Works  ---
    override<M extends keyof this>(method: M, newFunc: this[M]) {
    }

    //  Works  ---
    override2<M extends keyof this>(method: M, newFunc: (...args: Parameters<this["myMethod2"]>) => ReturnType<this["myMethod2"]>) {
    }

    //  Fails  ---
    override3<M extends keyof this>(method: M, newFunc: (...args: Parameters<this[M]>) => ReturnType<this[M]>) {
    }
}

const test = new Test();

test.override("myMethod2", a => "xxx");
test.override2("myMethod2", a => "xxx");
test.override3("myMethod2", a => "xxx");

Expected behavior:
I would expect override3 to compile and be equivalent to override + override2 (when M == "myMethod2")

Actual behavior:
override3 causes syntax error:

Type 'this[M]' does not satisfy the constraint '(...args: any) => any'.
  Type 'this[keyof this]' is not assignable to type '(...args: any) => any'.
    Type 'this[string] | this[number] | this[symbol]' is not assignable to type '(...args: any) => any'.
      Type 'this[string]' is not assignable to type '(...args: any) => any'.

Playground Link:
https://www.typescriptlang.org/play/?ssl=28&ssc=14&pln=28&pc=15#code/MYGwhgzhAEAqCmEAu0DeAod1vQLYE8BZeJACwHsATACgEoAuaZAJwEsA7AczSxz+ZIBXZu2gAiACwAmMQG5e2AL6Y+BYmSpTqYRiw6cGTJGy48+-ISPHSx0ANTQw8vsoXQA9O+wB1cswDWMNAAtKFu5ABu8MxslPAAPITQ8AAeSPDslDD+8PjkAGbQZKwQAHzUuCQUlIyEADTQ7PAA7gBiguzAjMUQANqEALq0ZjiufJ4+foHYocHhUTGscVKJyWkZWdA5eYU95ZUaNdD1jS3tnYzUAHQ3YMycEIwACndgB9EQ8T29YmpVmmIBqVhgBeUrQABKlnYsHwAAcEt9fkR-pQZEDhhgXCocBNoK0wKwQEFZvNorF4ABmVapdKZbK5ApFUglfao2oNJptDpdaDXW73R7QF7MN4kD5fFl9QbA6BgyHQ2EIyUlfoYkZKdCuYDkdjIIqIFAg07NOCGujydDpZBXSLkpbwajI9TVGQNMBy8FiFI+sS0eTWpC2hYUrTO1Fuxye8Q+lJ+gOG4P2uKUp1-Q6Rj3y72+-3oIA

Related Issues:

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 la reproduction liée dans TypeScript Playground et examinez la manière dont le compilateur traite Parameters et ReturnType lorsque T est le générique indexé this[M]. L’issue est terminée lorsque l’exemple override3 se compile avec l’inférence attendue pour myMethod2 et que le comportement est couvert par un test approprié du compilateur.

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é
5/5
Temps estimé
Plus d'une semaine
Activité
À l'abandon
Clarté
Plutôt claire
Accessibilité débutants
25/100

Recevez les nouvelles issues par e-mail

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