Unable to Extend Class from Dictionary of Classes

523 Views Asked by At

ISSUE:

I am attempting to return a class declaration from a higher order class method that extends a class from a class map {"SOME_CLASS" : SomeClass} thats key is a parameter of the higher order class method. However Typescript is throwing this error...
NOTE: I am not using any external libraries.

ERROR:

Cannot use 'new' with an expression whose type lacks a call or construct signature.

ATTEMPTS:

I have attempted to cast the Class Type as "Newable" however I lose my type binding for the class that is being extended.

SNIPPET

/// Contrived Example
interface BaseConfig {
  options: {
    profileHref: string
  }
}
abstract class Base { /*< BaseClass Implementations >*/
}
/// Example Class 1
class ProfileIcon extends Base {
  constructor(config: {
    options: {
      profileHref: string
    }
  }) {
    super(config);
  }
}
/// Example Class 2
class ScriptBlock extends Base {
  constructor(config: {
    options: {
      src: string
    }
  }) {
    super(config);
  }
}
}

class Manager {
  protected STORE: RootStore;
  constructor() {}
  public dispatchNewElement(elementType: keyof typeof ELEMENT_MANIFEST) {
    const storeShard = this.STORE.shard();
    const elementClass = ELEMENT_MANIFEST[elementType];


    /*
    //// ERROR: type 'typeof ProfileIcon | typeof ScriptBlock' is not a constructor function type.
    /// NOTE: 
    >> const T = new elementClass(...args)
    >> throws - Cannot use 'new' with an expression whose type lacks a call or construct signature.
    ////
    ////
    */

    return class extends /*err*/ elementClass /*endErr*/ {
      protected STORE_SHARD: typeof RootStore;
      constructor(elementConfig: { < unique fields to class implementation >
      }) {
        super(elementConfig);
        this.STORE_SHARD = storeShard;
      }
    }
  }

  /// Element Class Dictionary
  const ELEMENT_MANIFEST = {
    "PROFILE_ICON": ProfileIcon,
    "SCRIPT_BLOCK": ScriptBlock
  }

Please forgive any mis-formatting, this is maybe my second post on stack overflow. Cheers!

UPDATE from Comments

example of class returning class extending another class

class Master {
    public STATE: any;
    constructor() {
        this.STATE = { name: "foo" };
    }
    public dispatchNewClass(classType: string) {
        const myRefImage = Img;
        ////
        /* Works as a refVariable however..
        if i declare like...
            const myRefImage: Icon | Img
        I will get  
        >> Type 'typeof Img' is not assignable to type 'Icon | Img'.  
        >> Type 'typeof Img' is not assignable to type 'Img'.  
        >>Property 'TagName' is missing in type 'typeof Img'.
        */
        ///
        const asObject {}
        const ROOT = this.STATE;
        return class Slave extends myRefImage {
            protected ROOT: typeof ROOT;
            constructor(tagName: string) {
                super(tagName as "img")
                this.ROOT = ROOT;
            }
        }
    }
}

1

There are 1 best solutions below

7
On

That would not work in TypeScript.

The type system in TypeScript only exists at compile time.

When the code is compiled, it is pure JavaScript.

You cannot define a new class and/or extends it from another class that is known only at runtime.