Typescript 重新键入函数表达式的对象,跳过第一个函数参数
我目前有以下变量Typescript 重新键入函数表达式的对象,跳过第一个函数参数,typescript,typescript-typings,typescript-generics,Typescript,Typescript Typings,Typescript Generics,我目前有以下变量 const storePattern={ 声明:{ }, 突变:{ }, 行动:{}, 模块:{ 模态:{ 行动:{ OpenModel(存储,名称:字符串):布尔值{ console.log('打开模式',名称); 返回true; }, closeModal(存储,名称:字符串):布尔值{ console.log('关闭模式',名称); 返回true; }, } } } }; 显示:当前storePattern.modules.modal.actions的类型为 (属性)操
const storePattern={
声明:{
},
突变:{
},
行动:{},
模块:{
模态:{
行动:{
OpenModel(存储,名称:字符串):布尔值{
console.log('打开模式',名称);
返回true;
},
closeModal(存储,名称:字符串):布尔值{
console.log('关闭模式',名称);
返回true;
},
}
}
}
};
显示:当前storePattern.modules.modal.actions的类型为
(属性)操作:{
OpenModel(存储:任意,名称:字符串):布尔;
closeModal(存储:任意,名称:字符串):布尔;
}
目标:我希望重新定义类型,使其跳过第一个参数,成为:
(property) actions: {
openModal(name: string): boolean;
closeModal(name: string): boolean;
}
如果actions
是一个独立变量,那么我已经实现了以下功能
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]>;
}
我仍在学习typescript,所以我的方法不干净,也很乐意接受任何反馈:)我想这就是你想要的:
type StorePatternModules = typeof storePattern['modules'];
type StorePattern = {
state: any;
getters: any;
mutations: any;
actions: {
[TModule in keyof StorePatternModules]: {
[TModuleKey in keyof StorePatternModules[TModule]]: {
[K in keyof StorePatternModules[TModule][TModuleKey]]: (arg: Parameters<StorePatternModules[TModule][TModuleKey][K]>[1]) => ReturnType<StorePatternModules[TModule][TModuleKey][K]>
}
}
};
};
type StorePatternModules=typeof storePattern['modules'];
类型StorePattern={
国家:任何;
获得者:任何;
突变:任何;
行动:{
[tStorePatternModules键中的模块]:{
[TModuleKey in keyof StorePatternModules[TModule]]:{
[K in-keyof-StorePatternModules[TModule][TModuleKey]]:(参数[1])=>ReturnType
}
}
};
};
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'.
type StorePatternModules = typeof storePattern['modules'];
type StorePattern = {
state: any;
getters: any;
mutations: any;
actions: {
[TModule in keyof StorePatternModules]: {
[TModuleKey in keyof StorePatternModules[TModule]]: {
[K in keyof StorePatternModules[TModule][TModuleKey]]: (arg: Parameters<StorePatternModules[TModule][TModuleKey][K]>[1]) => ReturnType<StorePatternModules[TModule][TModuleKey][K]>
}
}
};
};