#A class can only implement an object type or intersection of object types with statically known ...

7 messages · Page 1 of 1 (latest)

weary fiber
#

Hi,
In my code, I often want to have a type that dynamically lists properties ({[k in UK]:...), and a class that is required to implement them. But I keep getting the error:

'A class can only implement an object type or intersection of object types with statically known members.'

Is there any way around this?
It would be super convenient to have this kind of flexibility.

short wadi
#

are you trying to do something like

type Foo = { foo: "foo" } | { bar: "bar" };

class Baz implements Foo {

}
```?
#

@weary fiber

weary fiber
#
type IA<UMethod extends string> = {[k in UMethod]:any}

//My dream : 

abstract class AA<UMethod extends string> implements IA<UMethod>{
    constructor(public a:string){}
    someOtherMethodWithA(){
        return this.a 
    }
}


class A extends AA<"test"> {
    constructor( a:string){
        super(a)
    }
    test(){
        return this.someOtherMethodWithA()
    }
} 

//Reality :

/*
class AABis {
    constructor(public a:string){}
    someOtherMethodWithA(){
        return this.a 
    }
}


class ABis extends AABis implements IA<"test"> {
    constructor( a:string){
        super(a)
    }
    test(){
        return this.someOtherMethodWithA()
    }
}
*/
#

The reason I need to do this is because I don’t want to force everyone to always remember to import both the interface and the parent class — I’d like to have everything together

wheat kraken
#

is there more going on in IA in your real code? if not, maybe you don't even need the implements and type parameter:

trail bridgeBOT
#
mkantor#0

Preview:```ts
type IA<UMethod extends string> = {[k in UMethod]: any}

abstract class AA {
constructor(public a: string) {}
someOtherMethodWithA() {
return this.a
}
}

class A extends AA {
constructor(a: string) {
super(a)
}
test() {
return this.someOtherMethodWithA
...```