farwayer / mst-decorators

Class based MobX-State-Tree definitions
26 stars 1 forks source link

Translating a model #8

Open djMax opened 4 years ago

djMax commented 4 years ago

We're evaluating using this library for a large project and I'm having some trouble getting started. My existing model is fairly simple:

export const FeatureStoreModel = types
  .model('Feature', {
    status: types.optional(
      types.enumeration<LoadStatus>(Object.values(LoadStatus)),
      LoadStatus.None,
    ),
    features: types.array(types.frozen<Feature>()),
  })
  .actions((self) => ({
    importFeatures: (features: Feature[]) => {
      self.status = LoadStatus.Complete;
      self.features.replace(features);
    },
  }));

LoadStatus is a TS enumeration, and Feature is a fairly complex TypeScript interface which comes from a automated api-type-generator (i.e. I don't want to have to copy it or modify it). When I do what I thought was the simple translation:

class FeatureStore {
  @enumeration(LoadStatus) status
  @array(@frozen(Feature)) features
}

I get an error that "Feature is used as a type when a value is expected." I assume that it's because Feature is supposed to be annotated with decorators just like FeatureStore is?

farwayer commented 4 years ago

Remove @ before frozen. It should be @array(frozen(Feature))

farwayer commented 4 years ago

Ah, Is Feature not MST type? In this situation you should not pass it as arg for frozen. mst-decorators has very basic TS defs for now. All improvements are welcome. For now you can only do @array(frozen()) features

djMax commented 4 years ago

yes, exactly. I figured that was the problem, though I wasn't sure what to do about it. Thanks.