microsoft / microsoft/TypeScript

Parameters<T> and ReturnType<T> limitation

Aperta
#38,685 2 commenti 9 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Awaiting More Feedback Suggestion
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
2g 4h
PR unite (30g)
132

Descrizione

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:

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia dalla riproduzione collegata in TypeScript Playground e analizza come il compilatore gestisce Parameters e ReturnType quando T è il generico indicizzato this[M]. L’issue è completata quando l’esempio override3 viene compilato con l’inferenza prevista per myMethod2 e il comportamento è coperto da un test appropriato del compilatore.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
typescript
Ambito
compilers
Tipo di issue
Bug
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
25/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.