Home > Net >  Typescript - ReturnType from nested arrow function
Typescript - ReturnType from nested arrow function

Time:05-04

I have a type that describes an object with a nested function:

type EntitiesGetters = {
  getCategories: (state: EntitiesState) => (panelID: EntityID) => CategoryGroup;
};

and here is an example of applying this type to an object:

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;
  },
};

How can I get the type I need in GET_TYPE (which corresponds to the type "CategoryGroup")?

Еither correct me if I'm using the wrong approach.

CodePudding user response:

You should be able to use the TypeScript builtin ReturnType:

export const entitiesGetters: EntitiesGetters = {
  getCategories: (state) => (panelID) => {
    const data: ReturnType<ReturnType<EntitiesGetters["getCategories"]>> = {}

    // ...

    return data;
  },
};
  • Related