Skip to content

mapActions: property does not exist on type #2

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

Closed
amoe opened this issue Oct 28, 2019 · 2 comments
Closed

mapActions: property does not exist on type #2

amoe opened this issue Oct 28, 2019 · 2 comments

Comments

@amoe
Copy link

amoe commented Oct 28, 2019

The helper mapActions seems to suffer from the same problem as the problem described with mapState and mapGetters. I'm betting that the solution described there also works for mapActions, but I'm not exactly sure what form it should take.

For instance, marking the action as any will silence the error, but I feel like there's a more correct type for actions. It might be good to have an example of the right type for actions.

import Vue, {VueConstructor} from 'vue';
import {mapActions} from 'vuex';

// Without this interface you get the following error
// 19:26 Property 'myAction' does not exist on type 'CombinedVueInstance<Vue, unknown, { doIt(): void; }, unknown, Readonly<Record<never, any>>>'.
interface VuexBindings {
    myAction: any;
}

export default (Vue as VueConstructor<Vue & VuexBindings>).extend({
    created() {
        console.log(this.myAction);
    },
    methods: {
        doIt() {
            console.log("doing a thing");
        },...mapActions(['myAction'])
    },
});
export default new Vuex.Store({
    state: {
    },
    mutations: {
    },
    actions: {
        myAction(store) {
            console.log("doing action");
        }
    },
    modules: {
    },
});
@ffxsam
Copy link
Owner

ffxsam commented Oct 28, 2019

Hey @amoe! On a basic level, this would work:

interface VuexBindings {
  someAction: ({ name, url }: { name: string; url: string }) => boolean;
}

And then you'd call it:

this.someAction({ name: 'GitHub', url: 'https://github.com' });

Though it's a bummer to have to spell out that function type in every .vue file you want to use it in. You could export your function and do it this way:

import { someAction } from '@/store/actions';

interface VuexBindings {
  someAction: typeof someAction;
}

But I'm not a fan of that. I'll leave this issue open until there's an actual good solution. 😄

@HIMISOCOOL
Copy link

vuex has next to no typescript support, its a real pain.
I have on my most recent project, cannibalized this pr from kstn
vuejs/vuex#1121
There's a bit of work to make it compile in newer ts versions and I modified it from there to make it work better for the class style syntax.
Ill try clean it up and post a gist here in the weekend but if you want results now you can try hack away at it

@ffxsam ffxsam closed this as completed Mar 19, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants