Skip to content

ThisParameterType not resolved in Generic Class #38482

@kwasimensah

Description

@kwasimensah

TypeScript Version: 3.7.x-dev.201xxxxx

Search Terms:

bind this Generic classes

Code

interface FooFunction<Args extends any[]> {
  (...args: Args): void;
}

export class Foo<Args extends any[]> {
  private currentTimeout: number | undefined;
  constructor(private readonly wrappedFunc: FooFunction<Args>) {
    const originalFooCaller = this.fooCaller;
   // Ideally you can leave out this line.
    this.fooCaller = this.fooCaller.bind(this as ThisParameterType<typeof originalFooCaller>);
    // Fails because it won't realize this and ThisParameterType<typeof originalFooCaller> are the same type
    // I *think* because it won't resolve  ThisParameterType until Args is known.
    //this.fooCaller = this.fooCaller.bind(this); 
  }

  fooCaller(...args: Args): void {
    this.wrappedFunc.apply(undefined, args);
  }
}

Expected behavior:

I don't need the explict cast to ThisParameterType

Actual behavior:

The ThisParameterType used by CallableFunction.bind doesn't match this.

Playground Link:
https://www.typescriptlang.org/play?#code/JYOwLgpgTgZghgYwgAgGIHt2oK4gWYdEAHgEEoBzAZ2QgA9IQATGuEATwG0BdAPmQDeAKGTIAFADopcSlQBcyctQCUCgG7pgTANxCAvkKH0ADuihhkCADZwqNDOjKzaDCM1Yce-YaONRganCQlthQUG5gACrAALYQ6NhgCiDYMQBG0MgAPsi4TBAwoBA6IpZEVGBQ2PhmYn4BQSjhcExEVuzIAO5QcMbGxTh4Cg6D+IQkSlS8yoKlogjlFmbAFKBwVg4AwutWmQC8yGAAFsBUEjCY21a7ULqiosen55c7+4cnZxfoVzcSaaBMMSPVg0SIfAAKMjgcUgUEi7H6xDACPiMGQy1WIHWW1eUGmd3uAHpCWg4MArDQMgg4NgqChgBZOkQAOQWZpWYAALxQwOQbCYyDBp0hPRh0HhiOR-XQaIxaw2L2u0H4Mh5RxQVGhPJRc2QxOQAElkAAqR4gADWxuQVJpdOQDK6LLZECo6CsahQgohULFcJRuXA5MUzlOyHNIHQnRAEl1xOBz2+uOQB3jXx+0D+AKBH2U2mQpQMpTTuMk0lkCkmqmQGi0s3u7ye3V6-SYowkzfaYjyBSKTAANHzZLmC-ogA

Related Issues:

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs InvestigationThis issue needs a team member to investigate its status.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions