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

Generic for abstract class #33491

Closed
hasezoey opened this issue Sep 18, 2019 · 5 comments
Closed

Generic for abstract class #33491

hasezoey opened this issue Sep 18, 2019 · 5 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@hasezoey
Copy link

I know this has been discussed, but i couldnt find a solution in these issues, so my use case is as following:

abstract class SomeClass {
  public array: object[]

  public async addCar<T extends SomeClass>(this: InstanceType<T> & mongoose.Document, car: object) {
    this.array.push(car);
  }
}

throws an typescript errors because An abstract constructor type can not be assigned to a non-abstract constructor type ts(2344), but InstanceType<T> & mongoose.Document cannot be replaced by something like Function, so i wanted to ask if there is currently a solution to this

using:

  • typescript 3.6.2
  • node 12.10.0
  • @types/node 8.10.53
  • target: es6
  • lib: esnext

PS: sorry if this is an question that should be on SO

@RyanCavanaugh RyanCavanaugh added the Working as Intended The behavior described is the intended behavior; this is not a bug label Sep 18, 2019
@RyanCavanaugh
Copy link
Member

This is the intended behavior.

@hasezoey
Copy link
Author

This is the intended behavior.

why is this "as intended"? when using an abstract class as an base / "extends" it should work, shouldnt it?

@RyanCavanaugh
Copy link
Member

RyanCavanaugh commented Sep 18, 2019

If it was allowed for abstract classes to be assignable to non-abstract construct signatures, then you could instantiate an abstract class through a trivial indirection.

@hasezoey
Copy link
Author

is there then a workaround?

@typescript-bot
Copy link
Collaborator

This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

3 participants