istrib / vuex-typescript

A simple way to make Vuex type-safe with intuitive intellisense
237 stars 18 forks source link

GetterHandler type missing parameters? #11

Open zerosym opened 6 years ago

zerosym commented 6 years ago

I've been trying out this approach but ran into an issue with specifying a vuex getter with more than just the state; i.e. (state, getters, rootState, rootGetters).

Shouldn't the following be GetterHandler<state: TModuleState, getters: any, rootState: TRootState, rootGetters: any) instead? I just started using Typescript so I don't know if I'm missing something. https://github.com/istrib/vuex-typescript/blob/master/src/index.ts#L12

skateborden commented 6 years ago

Were you able to get this to work?

d1820 commented 6 years ago

I also am trying to get this to work.. seems even the defined signature does not work correctly, RootState is also not injected into the read..

skateborden commented 6 years ago

I'm trying to remember the exact context of this, but I found a workaround for accessing the root state from within a module that might be helpful to others. In my index.ts I export my root vue instance export const vueApp = new Vue({ ... and then in the vuex module you can import { vueApp } and then call the static functions of another module by passing them vueApp.$store. It doesn't feel ideal but it works.

d1820 commented 6 years ago

OK after some digging its a bug in Vuex-Typescript, but you can get around the issue by doing this

get currentLanguage(): string | undefined {
// this._storeFront.getters.currentLanguage points to private method below
        return read(this._storeFront.getters.currentLanguage).call(this, this.store);
    }

...

private _getCurrentLanguage(...args: any[]): string | undefined {
        const state = args[0];
        const rootState: IRootState = args[1];
        const rootStore: any = args[2];
        const rootGetters: any = args[3];
        console.log('rootGetters', rootGetters);
        ...
    }

basically Vuex-Typescript does not pass all the params and the interface they have defined is not what vuex supports, but under the covers its still vuex and that still passes the parameters correctly. The above passed Typescript checking and stuff.

jackkoppa commented 5 years ago

@d1820, @skateborden - I know you've likely moved on from this issue. But could you check out #22 to see if you agree with the solution? I didn't realize that the bug I was trying to fix had already been documented here 😀

jackkoppa commented 5 years ago

Hi all - I realize very few people are likely going to be coming across this, but since this repo hasn't been updated since 11/13/17, I decided to fork & publish a maintained version. You can find that here: https://github.com/jackkoppa/typesafe-vuex

It includes the fix for this bug, + specs to make sure you can use GetterHandler methods that still access root state correctly (my PR for that on this repo is here). I plan on keeping it the exact same as this package otherwise, since I've found no other issues up to this point.