#How to type function parameter to accept a class not the instance
1 messages · Page 1 of 1 (latest)
you can make X an abstract class though, so users won't be able to create an X in the first place
they could accidentally pass an incompatible property though
to solve that you may be able to do protected abstract a: string. not sure whether that works though
ohhhh
!ts
class X {}
class Y extends X {
static staticName = 'name'
}
function func(arg: new (...args: any[]) => X){ // this
console.log(Y.staticName)
}
func(Y) // pass
func(new Y()) // this should be error
// ^^^^^^^
// Argument of type 'Y' is not assignable to parameter of type 'new (...args: any[]) => X'.
// Type 'X' provides no match for the signature 'new (...args: any[]): X'.```
like this?