Let's say we have some Java Node, StringNode and NodeFactory classes:
public class Node<T> {
private T value;
public void init(T value){
this.value = value;
}
}
public class StringNode extends Node<String> {}
public class NodeFactory {
public static <T> Node<T> createNode(Class<? extends Node<T>> nodeClass, T value) throws Exception {
Node<T> node = nodeClass.getConstructor().newInstance();
node.init(value);
return node;
}
}
So NodeFactory.createNode(StringNode.class, "value1"); would create a StringNode with the value: "value1".
How could I write such a factorymethod in typescript?
I tried to write a factorymethod in typescript:
public static createNode<T>(NodeClass: typeof Node<T>, value: T): Node<T> {
const node = new NodeClass();
node.init(value);
return node;
}
but does not accept typeof Node<T> generic types. So I tried putting in the method's type arguments:
public static createNode<T, N extends Node<T>>(NodeClass: typeof N, value: T): Node<T> {
But the compiler does not accept that either: typeof N: TS2693: 'N' only refers to a type, but is being used as a value here
Is there a typesafe way to write such a factorymethod in typescript?