3

I am getting the error:

"Member 'someMutation' implicitly has an 'any' type.".

This error appears in my Vue Component in Component.ts file for the mutation, received from Vuex store.

I have the following code in Component.ts:

@WithRender
@Component({
    components: { SomeChildComponent },
})
export default class Component extends Vue {
    constructor() {
        super();
        this.someFunc(this.someComponentMethod);
        // ... some logic
    }
    @Mutation public someMutation;

    private someComponentMethod(str: string) {
        this.someMutation();
    }
}

Code in store for mutations:

const mutations: MutationTree<State> = {
    someMutation(state, name: string): void {
        state.prop = state.prop.filter((el: IEl): boolean => el.name !== name);
    },
};

const c = {
    state,
    getters,
    mutations,
};

const store = new Vuex.Store(c);

I tried some variants like @Mutation public someMutation: void; or @Mutation public someMutation(s: string): void;, but was not successful.

My intention is to solve the issue, not just to turn off the rule NoImplicitAny in tslint.json file.

1 Answer 1

1

Based on the source of someMutation, the method takes a string argument called name, and it returns void, which is typed like this:

(name: string) => void

Since the property is automatically initialized by vuex-class, we can use the definite assignment assertion to avoid the "no initializer" error.

!: (name: string) => void
^

The final result is:

@Mutation someMutation!: (name: string) => void
Sign up to request clarification or add additional context in comments.

2 Comments

Tony, thank you very much for the answer! This solution is absolutely resolve my issue!) You are Typescript Guru! Thanks again!:)
@Vika No problem :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.