microsoft / microsoft/TypeScript

Parameters<T> and ReturnType<T> limitation

Open
#38,685 2 comments 9 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Awaiting More Feedback Suggestion
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'.

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:

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.