microsoft / microsoft/TypeScript
Parameters<T> and ReturnType<T> limitation
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 132
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'.
Related Issues:
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the linked TypeScript Playground reproduction and inspect how the compiler handles Parameters and ReturnType when T is the indexed generic this[M]. The issue is done when the override3 example compiles with the expected inference for myMethod2 and the behavior is covered by an appropriate compiler test.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100