TypeScript: anonymouse class factory

My TypeScript v2.2.

I have this class factory:

  • Returning a generic array using Array.map (TypeScript)
  • How to annotate in javascript a class that extends a generic type, using closure compiler
  • TypeScript: Generics type definition for TypedArray
  • Type checking and generics
  • Default value of Type in Generic classes in Typescript
  • Typescript: Inject generic & get ES6 module name
  • export class A { name: string; }
    
    export function makeConstructor(name: string)
    {
      const newClass = class extends A { };
    
      newClass.prototype.name = name;
    
      return newClass;
    }
    

    TypeScript throw error:

    Return type of exported function has or is using private name ‘(Anonymous class)’.

    I can say that this factory returns any to hide error, but how I can explain what exactly returns?

    I tried to write

    • makeConstructor<T extends A>(name: string): T
    • makeConstructor<T extends typeof A>(name: string): T
    • makeConstructor<T extends A['prototype']>(name: string): T['prototype']

  • TypeScript: Generics type definition for TypedArray
  • Generic curry function with n-arguments in typescript
  • Propagate constructor signature to factory method
  • How to annotate in javascript a class that extends a generic type, using closure compiler
  • How to make base generic type know child generic in TypeScript?
  • Returning a generic array using Array.map (TypeScript)
  • One Solution collect form web for “TypeScript: anonymouse class factory”

    When you use the Factory design pattern, you should probably want to shield the exact implementation class chosen by the Factory, and just return A (or its interface) instead. Therefore, I think returning A just does the trick, no need for generics etc…