#How do I get a function from an abstract class to return an instance of a class that implements it?

5 messages · Page 1 of 1 (latest)

drifting lilyBOT
#
.popcorn#0

Preview:```ts
abstract class SomeParent {
abstract someFunc<C extends SomeParent>(): Promise<C>
}

class SomeChild implements SomeParent {
async someFunc() {
return new SomeChild()
}
}```

candid onyx
#

I'm not sure I explained this well, but essentially I can't seem to return 'SomeChild()' from my implementation of 'someFunc' because of an error I haven't seen before:

#
Property 'someFunc' in type 'SomeChild' is not assignable to the same property in base type 'SomeParent'.
  Type '() => Promise<SomeChild>' is not assignable to type '<C extends SomeParent>() => Promise<C>'.
    Type 'Promise<SomeChild>' is not assignable to type 'Promise<C>'.
      Type 'SomeChild' is not assignable to type 'C'.
        'C' could be instantiated with an arbitrary type which could be unrelated to 'SomeChild'.(2416)
mellow rune
#

SomeParent's someFunc is a function with generic parameters whereas SomeChild's someFunc has no generic parameters
and you don't need generic parameters to achieve what you want, just write Promise<SomeParent>

candid onyx
#

Argh you genius! Not using generics fixed everything.