0

I am attempting to write a test using vuex test utils. I have setup the test as such:

myModule.ts

export const mutations = {
  [TYPES.SET_VALUE1](state: myModuleState, payload: TYPE1) {
    //...
  },
  [TYPES.SET_VALUE2](state: myModuleState: payload: TYPE2) {
    //...
  },
  [TYPES.SET_VALUE3](state: myModuleState, payload: TYPE3) {
    //...
  },
  [TYPES.SET_VALUE4](state: myModuleState, payload: TYPE4) {
    //...
  }
};

myModule.spec.ts

import chai from 'chai';
import { expect } from 'chai';
import { mutations, myModule } from '@/store/modules/myModule';
import TYPES from '@/store/mutation-types';

const testValue:TYPE1 = {
  name: 123
};

describe('mutations', () => {
  it('TYPE1', () => {
    // mock state
    const state = {};
    // apply mutation
    mutations[TYPES.SET_VALUE1](state, testValue);
    // assert result
    expect(state).to.deep.equal(testValue);
  });
});

and when I run I am getting the following error:

TS2345: Argument of type 'TYPE1' is not assignable to parameter of type 'TYPE1 & TYPE2 & TYPE3 & TYPE4'.
  Type 'TYPE1' is not assignable to type 'TYPE2'.

It seems as though it expects the type of the argument to be all of the types at the same time? When I change the type of the argument, say from TYPE1 to TYPE2, the error changes to Type 'TYPE2' is not assignable to type 'TYPE1'. What am I doing wrong?

Edit: I changed myModule.spec.ts to myModule.spec.js and removed the typed def on testValue. and the test passes just fine.

John Snow
  • 1,898
  • 2
  • 27
  • 48
  • Only something that is both Type1 AND Type2 can be assigned to anything that's Type1 & Type2; You haven't mentioned where in this file you see the error so its almost impossible for people to help debug it for you but depending on the situation it's possible a type of Type1 AND Type2 cannot be achieved naturally and you'll have to cast an example of this is the type ("hello" & "goodbye") which cannot be achieved naturally – Shanon Jackson Jun 27 '19 at 23:36
  • Does this example code count as a [mcve]? Is `TYPES` defined somewhere we can see? Ideally if you want a good answer you'd remove as many external dependencies as possible and reproduce your issue in a standalone IDE... or barring that, give a link to a web IDE with the required dependencies set up. Otherwise all we can do is guess... my guess is that somehow the types of `TYPES.XXX` are not being interpreted as literal values, so `mutations[TYPES.XXX]` is being treated as a union of function types instead of the specific function type you expect. But I can't be sure without a [mcve]. – jcalz Jun 28 '19 at 00:37

0 Answers0