Typescript - 来自嵌套箭头函数的 ReturnType
Typescript - ReturnType from nested arrow function
我有一个描述带有嵌套函数的对象的类型:
type EntitiesGetters = {
getCategories: (state: EntitiesState) => (panelID: EntityID) => CategoryGroup;
};
下面是将此类型应用于对象的示例:
export const entitiesGetters: EntitiesGetters = {
getCategories: (state) => (panelID) => {
const data = {} as GET_TYPE;
for (const key in state.categories) {
const category = state.categories[key];
if (category.entities.panel === panelID) {
data[Object.keys(data).length] = category;
}
}
return data;
},
};
如何在GET_TYPE中获取我需要的类型(对应类型“CategoryGroup”)?
如果我使用了错误的方法,请纠正我。
您应该可以使用内置的 TypeScript ReturnType
:
export const entitiesGetters: EntitiesGetters = {
getCategories: (state) => (panelID) => {
const data: ReturnType<ReturnType<EntitiesGetters["getCategories"]>> = {}
// ...
return data;
},
};
我有一个描述带有嵌套函数的对象的类型:
type EntitiesGetters = {
getCategories: (state: EntitiesState) => (panelID: EntityID) => CategoryGroup;
};
下面是将此类型应用于对象的示例:
export const entitiesGetters: EntitiesGetters = {
getCategories: (state) => (panelID) => {
const data = {} as GET_TYPE;
for (const key in state.categories) {
const category = state.categories[key];
if (category.entities.panel === panelID) {
data[Object.keys(data).length] = category;
}
}
return data;
},
};
如何在GET_TYPE中获取我需要的类型(对应类型“CategoryGroup”)?
如果我使用了错误的方法,请纠正我。
您应该可以使用内置的 TypeScript ReturnType
:
export const entitiesGetters: EntitiesGetters = {
getCategories: (state) => (panelID) => {
const data: ReturnType<ReturnType<EntitiesGetters["getCategories"]>> = {}
// ...
return data;
},
};