championswimmer / vuex-module-decorators

TypeScript/ES7 Decorators to create Vuex modules declaratively
https://championswimmer.in/vuex-module-decorators/
MIT License
1.8k stars 170 forks source link

Treat 'undefined' as a valid state value #240

Open jamesfoster opened 4 years ago

jamesfoster commented 4 years ago

undefined is a valid initial value for a state property, however, this library excludes them from the state object. As such, any computed values which depend on this state do not react when the state is updated. The following line is the culprit:

https://github.com/championswimmer/vuex-module-decorators/blob/46257b09a8de8e6b615de0837fdc10ff74d85613/src/module/staticGenerators.ts#L13

I have tested the change to this library to allow undefined here and it has the desired effect! The property is correctly reactive as expected!

Example module:

@Module({
  dynamic: true,
  store,
  name: 'app-state',
})
export default class AppState extends VuexModule {
  drawer?: boolean = undefined;

  ...
}

Is there a way, currently, to support undefined that I'm missing?

One work around is to ensure all properties of your modules are not undefined:

  drawer: { value?: boolean } = { value: undefined }

or

  drawer: boolean | null = null;

Both of which require a change to the consuming code as they expect undefined. This feels like a hack.

I can create a Pull Request for the change if you want.

daividh commented 4 years ago

Duplicate of #35 ?