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
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.
Ahh, I see what you mean. My bad. You can do this
TLDR;
Does that handle your use case?
Oh man yes it does, that's exactly what I was looking for! I didn't realize that was a thing! Thanks!
Or even better using generics:
Generics for sure, but it still needs to be
typeof Modelas he doesn’t want an instance of the class.I missed that. In that case, he shouldn't use a
classhere, 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
typeis an option. It just depends on what he wants to do in that function. If aninterfaceortypeis used and he wanted to create an instance of theModelclass, using aninterfaceortypewould not work. If he wanted to use it for duck typing in his function, then aninterfaceortypewould suffice.Here's a sample TypeScript Playground for those interested.
Very interesting. I didn't realise you could combine
newwithtypeof Tparams.Yeah
Modelisn'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.