In my current setup, I have a variable called storePattern
const storePattern = {
state: {
},
mutations: {
},
actions: {},
modules: {
modal: {
actions: {
openModal(store, name: string): boolean {
console.log('Opening Modal', name);
return true;
},
closeModal(store, name: string): boolean {
console.log('Closing Modal', name);
return true;
},
}
}
}
};
Current Status: The types for
storePattern.modules.modal.actions
currently look like this
(property) actions: {
openModal(store: any, name: string): boolean;
closeModal(store: any, name: string): boolean;
}
Objective: My goal is to redefine the types so that it skips the first argument and becomes:
(property) actions: {
openModal(name: string): boolean;
closeModal(name: string): boolean;
}
I have found a solution that works when actions
is a standalone variable
const actions = {
openModal(store, name: string): boolean {
console.log('Opening Modal', name);
return true;
},
closeModal(store, name: string): boolean {
console.log('Closing Modal', name);
return true;
}
}
type TypedActions = {
[Property in keyof typeof actions]: (arg: Parameters<typeof actions[Property]>[1]) => ReturnType<typeof actions[Property]>;
}
However, the same approach does not work for the storePattern
object.
type StorePattern = {
state: any;
getters: any;
mutations: any;
actions: {
[ModuleProperty in keyof typeof storePattern['modules'] as ModuleProperty]: {
[ActionProperty in keyof typeof storePattern['modules'][ModuleProperty]['actions']]: (arg: Parameters<typeof storePattern['modules'][ModuleProperty]['actions'][ActionProperty]) => ReturnType<typeof storePattern['modules'][ModuleProperty]['actions'][ActionProperty]>;
}
};
};
Type '{ modal: { actions: { openModal(store: any, name: string): boolean; closeModal(store: any, name: string): boolean; }; }; }[ModuleProperty]["actions"][ActionProperty]' does not satisfy the constraint '(...args: any) => any'.
Type '{ modal: { actions: { openModal(store: any, name: string): boolean; closeModal(store: any, name: string): boolean; }; }; }[ModuleProperty]["actions"][keyof { modal: { ...; }; }[ModuleProperty]["actions"]]' is not assignable to type '(...args: any) => any'.
Type '{ modal: { actions: { openModal(store: any, name: string): boolean; closeModal(store: any, name: string): boolean; }; }; }[ModuleProperty]["actions"][string] | { ...; }[ModuleProperty]["actions"][number] | { ...; }[ModuleProperty]["actions"][symbol]' is not assignable to type '(...args: any) => any'.
Type '{ modal: { actions: { openModal(store: any, name: string): boolean; closeModal(store: any, name: string): boolean; }; }; }[ModuleProperty]["actions"][string]' is not assignable to type '(...args: any) => any'.
As I am still learning TypeScript, my approach may not be perfect. I'm open to any feedback as well :)