1

Complete Re-Edit

I have a variety of classes, using a common base class and implementing common interfaces, that are instantiated at run-time based on config files. Because they are dynamically created, by default there is no intellisense/auto-complete unless I use a type guard. Right now I am tagging the classes using a _interfaces prop. As a contrived example...

abstract class BaseClass {
    _interfaces: any[] = []
}

interface A {
    propA: string
    funcA: (arg1: number) => void
}

interface B {
    propB: string
    funcB: (arg1: string) => void
}

class ExampleClass extends BaseClass implements A, B {
    propA = 'someValue'
    funcA(arg1: number){
        console.log('do a thing')
    }
    propB = 'anotherValue'
    funcB(arg1: string){
        console.log('do another thing')
    }
    _interfaces = ['A', 'B']
}

let myExampleClass = new ExampleClass() //this part is done dynamically at runtime

The runtime check in the type guard is simple, but actually typing the check for intellisense/auto-complete purposes is proving to be tricky.

I have a complicated version of this that works, but I am not fond of it. Currently am using a "type of types" and a few overly complicated types from here and here. There is a playground link here, but it is not for the faint of heart

I could have upwards of 10 interfaces or more applied to some classes so checking for specific properties isn't really scalable here.

Is there a simpler, more generic way, to get run-time detection of interfaces and get intellisense/auto-complete from a type guard?

Updated

Made a simpler version based on @jcalz comments and other posts using his Intersectprops. Playground link here. I still don't quite follow how the Intersectprops type works though.

pure_bordem
  • 159
  • 3
  • 15

0 Answers0