Experimenting with generics in typescript led me to face a frustrating challenge with a perplexing error message from typescript.
My attempt to create a wrapper for generating classes with a common base class ended in encountering the following error message:
'T' only refers to a type, but is being used as a value here. ts(2693)
Below is the snippet of code where this issue arises:
class Test {
constructor(i: number) {
}
}
class Test1 extends Test {
}
class Test2 extends Test {
}
function f<T extends Test>(n:number): T {
return new T(n)
}
Although the following code works, it lacks generality:
function f1(n: number): Test1 {
return new Test1(n)
}
For further exploration, here's the typescript playground.
Can someone shed light on the confusion here and guide me on how to resolve it?
I also attempted creating an interface that specifies a mandatory CTOR, but it appears to be unsupported.