ThomasReggi
ThomasReggi

Reputation: 59345

Abstract class that requires abstract property

I am working with these dual abstract classes, and I am trying to require the second abstract class to have a property that is another abstract class. But it's failing.

abstract class Static<C extends {[key: string]: any}> {
    abstract doughnut: boolean
}

class AlphaStatic extends Static<{ love: true }> {
    doughnut = true
}

abstract class Dynamic<C extends {[key: string]: any}> {
    abstract static: Static<C>
}

class AlphaDynamic extends Dynamic<{ love: true }> {
    static = AlphaStatic
}

Playground

How can I fix this?

Property 'static' in type 'AlphaDynamic' is not assignable to the same property in base type 'Dynamic<{ love: true; }>'. Property 'doughnut' is missing in type 'typeof AlphaStatic' but required in type 'Static<{ love: true; }>'.ts(2416) Untitled-2(2, 14): 'doughnut' is declared here.

Upvotes: 2

Views: 1159

Answers (1)

ThomasReggi
ThomasReggi

Reputation: 59345

Was missing "new"

abstract class Static<C extends {[key: string]: any}> {
    abstract doughnut: boolean
}

class AlphaStatic extends Static<{ love: true }> {
    doughnut = true
}

abstract class Dynamic<C extends {[key: string]: any}> {
    abstract static: Static<C>
}

class AlphaDynamic extends Dynamic<{ love: true }> {
    static = new AlphaStatic()
}

Upvotes: 3

Related Questions