Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

This type inference of static property getter #50301

Closed
avin-kavish opened this issue Aug 14, 2022 · 3 comments
Closed

This type inference of static property getter #50301

avin-kavish opened this issue Aug 14, 2022 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@avin-kavish
Copy link

avin-kavish commented Aug 14, 2022

Bug Report

It's possible to infer the this type contextually on a static method so inherited classes get their own type as this when calling an inherited static method. But in static property getters it's not possible to specify generic parameters, so it's not possible to get the this type of a derived class in a parent getter.

In the example below, Bar.default returns an instance of Bar but it's type is inferred as Foo.

type Constructor<T> = { new (): T }

class Foo {
  static factory<T extends Foo>(this: Constructor<T>): T {
    return new this()
  }

  static get default() { // Not possible to specify type parameters
    return new this()
  }
}

class Bar extends Foo {}

const x = Bar.factory() // x is type Bar
const y = Bar.default // y is type Foo

// however, both are instances of Bar
console.log(x instanceof Bar) // true
console.log(y instanceof Bar) // true

Ideally, I think both getter and method should work without any extra type parameters needing to be specified. Static this type should be inferred contextually.

class Foo {
  static factory(): this {  // TS2526: A 'this' type is available only in a non-static member of a class or interface.
    return new this()
  }

  static get default(): this {
    return new this()
  }
}

Edit: I'm seeing some comments in other threads about new this() not being allowed because the caller's ctor signature isn't known in the parent class. That's understandable. This is a demonstration of the problem of type inference, this is not an actual use case. My actual use cases don't invoke any unknown constructors. So please don't dismiss this because you think the use case isn't viable.

🔎 Search Terms

  • static property getter
  • static property this
@whzx5byb
Copy link

Related: #5863.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Aug 17, 2022
@RyanCavanaugh
Copy link
Member

The solution for the specified problem here is #5863.

My actual use cases don't invoke any unknown constructors. So please don't dismiss this because you think the use case isn't viable.

We can't just do things and ignore their side effects...

@avin-kavish
Copy link
Author

avin-kavish commented Aug 18, 2022

I must have missed it. I don't see a solution for static getters. But yeah, we can discuss it there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants