0👍
✅
This is because you’re using P: string for your mutations and getters, so we don’t know what the specific keys are, and therefore their types can be inferred.
You should use extra generic parameters for P here – for the MutationKeys and GetterKeys, which can then be inferred correctly:
interface Options<
State,
MutationKeys extends string,
G extends Getters<State, string>
> {
state: State;
getters: G;
mutations: {
[P in MutationKeys]: (state: State, getters: G) => void;
};
}
type Getters<State, Keys extends string> = {
[key in Keys]: (state: State) => any;
};
function createStore<
State,
MutationKeys extends string,
GetterKeys extends string
>(options: Options<State, MutationKeys, Getters<State, GetterKeys>>) {
return options;
}
createStore({
state: {
count: 0
},
getters: {
isOdd: (state) => state.count % 2 === 1
},
mutations: {
incrementIfOdd(state, getters) {
if (getters.isOdd(state)) {
++state.count;
}
}
}
});
Source:stackexchange.com