You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I've got a project using mobx-keystone with a complex inheritance hierarchy, and I'm realizing I could simplify things if I switched to an inheritance pattern involving mixins.
The Typescript docs recommend an approach they call "constrained mixins", in which factory functions accept a base class constrained to a type which they extend and return. I'd like to figure out how to do something like this with ExtendedModel.
Here's where I'm at:
// A base classclassEntityextendsModel({}){}// An example factory that mixes in some functionalityfunctionmakeCountable<TextendsModelClass<Entity>>(Base: T){class_CountableextendsExtendedModel(modelClass(Base),{quantity: tProp(types.number,0)}){
@modelActionincrement(){this.quantity+=1}}return_Countable}// An attempt at describing the Countable type so I can use it// as a constraint below typeCountable=ReturnType<typeofmakeCountable>// A second factory which I'd like to be constrained to only accept classes which// have passed through `makeCountable` alreadyfunctionmakeProducer<TextendsModelClass<Countable>>(Base: T){// ^^^^^^^^^// Type 'typeof _Countable' does not// satisfy the constraint 'AnyModel// | AnyDataModelclass_ProducerextendsExtendedModel(modelClass(Base),{// etc.}){// etc.}return_Producer}
Is there a way to make this work? I suspect there's something I should be doing differently specifying the constraint on the type argument to the factories (T Extends ModelClass<etc>). Or it could be that I need some other way of describing the return type of the factories to use as constraints (e.g. type Countable = ReturnType<typeof makeCountable>).
Once I figure out a pattern that works, I'd be happy to contribute it back to the docs on inheritance if this would be useful, generally.
The text was updated successfully, but these errors were encountered:
I've got a project using
mobx-keystone
with a complex inheritance hierarchy, and I'm realizing I could simplify things if I switched to an inheritance pattern involving mixins.The Typescript docs recommend an approach they call "constrained mixins", in which factory functions accept a base class constrained to a type which they extend and return. I'd like to figure out how to do something like this with
ExtendedModel
.Here's where I'm at:
Is there a way to make this work? I suspect there's something I should be doing differently specifying the constraint on the type argument to the factories (
T Extends ModelClass<etc>
). Or it could be that I need some other way of describing the return type of the factories to use as constraints (e.g.type Countable = ReturnType<typeof makeCountable>
).Once I figure out a pattern that works, I'd be happy to contribute it back to the docs on inheritance if this would be useful, generally.
The text was updated successfully, but these errors were encountered: