#How to type function parameter to accept a class not the instance

1 messages · Page 1 of 1 (latest)

swift wind
#

not possible

#

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

swift wind
#

ohhhh

weak zodiacBOT
swift wind
#

!ts

weak zodiacBOT
#
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'.```
swift wind
#

like this?