microsoft / microsoft/TypeScript
Parameters<T> and ReturnType<T> limitation
Nessuno ha ancora preso questa issue.
- 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'.
Related Issues:
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 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