简体   繁体   中英

Typescript - ReturnType from nested arrow function

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM