Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A small fix in typings for StoreOptions #1231

Closed
MrVichr opened this issue Apr 13, 2018 · 2 comments
Closed

A small fix in typings for StoreOptions #1231

MrVichr opened this issue Apr 13, 2018 · 2 comments
Assignees
Labels
types Related to typings only

Comments

@MrVichr
Copy link

MrVichr commented Apr 13, 2018

Version

3.0.1

Reproduction link

https://codesandbox.io/s/l2wzjwp4vm

Steps to reproduce

Sorry I'm not going to spend several hours making a minimal demo project and uploading it to github just to have 8 letters added. The error and fix should be obvious.

// src/datamodel/appstore.ts
class TAppStore {
  counter: number=0;
};

export var AppStore=new Vuex.Store<TAppStore>({ 
  state: function() {
    console.log('creating AppStore'); //gets called just fine
    return new TAppStore();
  }
}

What is expected?

No errors reported on compilation.

What is actually happening?

ERROR in j:\types\CML\front\src\datamodel\appstore.ts
[tsl] ERROR in j:\types\CML\front\src\datamodel\appstore.ts(44,2)
      TS2345: Argument of type '{ state: () => TAppStore; }' is not assignable to parameter of type 'StoreOptions<TAppStore>'.
  Types of property 'state' are incompatible.
    Type '() => TAppStore' is not assignable to type 'TAppStore | undefined'.
      Type '() => TAppStore' is not assignable to type 'TAppStore'.
        Property 'counter' is missing in type '() => TAppStore'.

  Build failed with errors.

It seems that vuex/types/index.d.ts, in interface StoreOptions, is missing the possibility of using a function instead of an object.
This is the recommended way to initialize state: https://vuex.vuejs.org/en/modules.html (chapter Module Reuse, at the very bottom).
An easy fix would be editing the vuex/types/index.d.ts StoreOptions:

export interface StoreOptions<S> {
- state?: S;
  state?: S|(()=>S);
  getters?: GetterTree<S, S>;
  actions?: ActionTree<S, S>;
  mutations?: MutationTree<S>;
  modules?: ModuleTree<S>;
  plugins?: Plugin<S>[];
  strict?: boolean;
}
@ktsn
Copy link
Member

ktsn commented Apr 14, 2018

Thanks. There is a PR already #1132

@ktsn ktsn self-assigned this Apr 16, 2018
@ktsn ktsn added the types Related to typings only label Apr 16, 2018
@ktsn
Copy link
Member

ktsn commented Oct 8, 2018

#1132 is already merged.

@ktsn ktsn closed this as completed Oct 8, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
types Related to typings only
Projects
None yet
Development

No branches or pull requests

2 participants