ThomasReggi
ThomasReggi

Reputation: 59365

Abstract static method on abstract class

I remember being able to provide an interface for a abstract class, something like this, but I can't find that code online anywhere. I am I unable to do it here:

interface AbstractParent { 
  staticMember: () => boolean
}

abstract class AbstractParent {  
  static example() { 
    return this.staticMember()
  }
}

class Utilize extends AbstractParent {  
  static staticMember() { 
    return true
  }
}

console.log(Utilize.example())

Playground

Is it possible to have an abstract class require a static method?

Upvotes: 2

Views: 265

Answers (2)

Maxim Mazurok
Maxim Mazurok

Reputation: 4138

You can also try replacing this.staticMember() with Utilize.staticMember(): Playground

Ideally, you want to use static abstract staticMember. But unfortunetly, it's not yet supported and doesn't seem like there's a good way to implement this.

Check our these discussions:

Upvotes: 2

ThomasReggi
ThomasReggi

Reputation: 59365

This works without error, but does not ensure that Utilize implements a staticMember method.

abstract class AbstractParent {  
  static staticMember: () => boolean
  static example() { 
    return this.staticMember()
  }
}

class Utilize extends AbstractParent {  
  static staticMember() { 
    return true
  }
}

console.log(Utilize.example())

Upvotes: 0

Related Questions