I'm a fan of Open Source and have a growing interest in serverless and edge computing. I'm not a big fan of spiders, but they're doing good work eating bugs. I also stream on Twitch.
I rarely use classes aside from React components (pre-hooks) as I prefer functions even though I'm not a full FP kind of person, but they are still used by many. 😉
For sure, a type is an option. It just depends on what he wants to do in that function. If an interface or type is used and he wanted to create an instance of the Model class, using an interface or type would not work. If he wanted to use it for duck typing in his function, then an interface or type would suffice.
interfaceSomeModelInterface{someMethod:()=>boolean;}classModelimplementsSomeModelInterface{someMethod(){returntrue;}}functionhandleRelations(modelClass:typeofModel){constinstance=newmodelClass();constvalue=instance.someMethod();// true;}functionhandleRelations2(modelClass:SomeModelInterface){constinstance=newmodelClass();// errorsconstvalue=modelClass.someMethod();// All good}
Full-time web dev; JS lover since 2002; CSS fanatic. #CSSIsAwesome
I try to stay up with new web platform features. Web feature you don't understand? Tell me! I'll write an article!
He/him
Yeah Model isn't my class, it's provided by Objection.js, an ORM library I'm using. As mentioned in the post, I need to access static properties of various subclasses of Model, so I need to pass around the classes themselves.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
I missed that. In that case, he shouldn't use a
class
here, rather:I rarely use classes aside from React components (pre-hooks) as I prefer functions even though I'm not a full FP kind of person, but they are still used by many. 😉
For sure, a
type
is an option. It just depends on what he wants to do in that function. If aninterface
ortype
is used and he wanted to create an instance of theModel
class, using aninterface
ortype
would not work. If he wanted to use it for duck typing in his function, then aninterface
ortype
would suffice.Here's a sample TypeScript Playground for those interested.
Very interesting. I didn't realise you could combine
new
withtypeof T
params.Yeah
Model
isn't my class, it's provided by Objection.js, an ORM library I'm using. As mentioned in the post, I need to access static properties of various subclasses ofModel
, so I need to pass around the classes themselves.