I'm currently diving into the realm of Vuex Method-Style getters and seeking clarity on how to properly implement them. Here's a reference Fiddle along with the code snippet:
interface IData {
name: string;
}
type IGetters = {
data(getters: IGetters): IData;
getDataByKey: (getters: IGetters) => (name: string) => string;
getName(getters: IGetters): string;
}
const getters: IGetters = {
data(getters) {
return {
name: 'Hello'
};
},
getDataByKey: (getters) => (name) => {
return getters.data[name as keyof typeof getters.data];
},
getName(getters) {
return getters.getDataByKey('name');
}
}
The hurdle I'm facing mainly revolves around the getName
function. Whenever I try to call getDataByKey
, TypeScript throws error TS-2345 indicating that an 'Argument of type 'string' is not assignable to parameter of type 'IGetters'.' It seems like the misconception lies in the expected calling format which contrasts Vuex structure.
After consulting this insightful SO response, I attempted to incorporate the suggestions but couldn't get it to work effectively. Any advice or recommendations from experienced developers would be greatly appreciated!