How can I define the return type of a lodash reduce function with Typescript?

user3568783

I am trying to define the output of this Typescript function.

function (data: { topicId: number; subTopicId: number; topicName: string; subTopicName; string; }[] ) {
            var output = <IAnything>{
                dataMap: _.reduce(data, function (rv, v) {
                    rv[v.subTopicId] = v;
                    return rv;
                }, {});

I could map some parts which I didn't include in this question but I am getting confused on how to make the dataMap field. Can someone help me and tell me how I can map the output of the lodash _.reduce in the interface below. From what I can see the output of this reduce is:

data: { topicId: number; subTopicId: number; topicName: string; subTopicName; string; }[]

but how can I represent this and how can I represent the subTopicId that is used for the index of the array?

interface IAnything {
    //data: { id: number; name: string; }[];
    dataMap: 
}

Here is what the dataMap output looks like:

{
 "1":{"topicId":1,
      "subTopicId":1,
      "topicName":"x",
      "subTopicName":"x"},
 "2":{"topicId":1,
      "subTopicId":2,
      "topicName":"x",
      "subTopicName":"x"},
 "62":{"topicId":10,
       "subTopicId":62,
       "topicName":"x",
       "subTopicName":"x"}
}
David Bohunek

Your interfaces should look like this:

interface IAnything
{
    dataMap: IMap
}

interface IData
{
    topicId: number;
    subTopicId: number;
    topicName: string;
    subTopicName; string;
}

interface IMap{
    [key: string] : IData;
}

Then your function would look like this:

function (data: IData[]){
    var output = <IAnything>{
        dataMap: _.reduce(data, function (rv: IData, v: IData)
        {
            rv[v.subTopicId] = v;
            return rv;
        }, {})
    };
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I define the return type of a lodash reduce function with Typescript?

From Dev

How can I define a return type of void for a function in a Typescript interface?

From Dev

How can I define a Typescript object return value for a function?

From Dev

How can I define a Typescript interface for a function?

From Dev

Can I define a Typescript function parameter to have a type of boolean or string?

From Dev

How can I change the way a lodash function is used for Typescript?

From Dev

How can I change the way a lodash function is used for Typescript?

From Dev

How can I inject a type into a function in Typescript?

From Dev

How can I inject a type into a function in Typescript?

From Dev

Typescript - How can I conditionally define a type based on another property

From Dev

How can I change the return type of this function?

From Dev

How can I specify a return type for a $http call with Typescript?

From Dev

In typescript, how to define type of async function

From Dev

Typescript: How to define overloaded function type

From Dev

How can I define a Rust function type which returns its own type?

From Dev

How can I reduce boilerplate from this function?

From Dev

How can I reduce boilerplate from this function?

From Dev

Swift: How can I make a function with a Subclass return type conform to a protocol, where a Superclass is defined as a return type?

From Dev

How to define a function which cannot return anything in TypeScript

From Java

Typescript: Can I define an n-length tuple type?

From Dev

Typescript - Function return type if I want to return object

From Dev

Can I define a function pointer pointing to an object of std::function type?

From Dev

How can I return a function?

From Dev

How can I return a function?

From Dev

How can I define the LAMBDA function in LISP?

From Dev

How can I define a function in JavaScript?

From Dev

How can I define a paramter in a function.

From Dev

How can I write a function have a polymorphic return type based on the type argument of its type parameter?

From Dev

How can I type-hint a function where the return type depends on the input type of an argument?

Related Related

  1. 1

    How can I define the return type of a lodash reduce function with Typescript?

  2. 2

    How can I define a return type of void for a function in a Typescript interface?

  3. 3

    How can I define a Typescript object return value for a function?

  4. 4

    How can I define a Typescript interface for a function?

  5. 5

    Can I define a Typescript function parameter to have a type of boolean or string?

  6. 6

    How can I change the way a lodash function is used for Typescript?

  7. 7

    How can I change the way a lodash function is used for Typescript?

  8. 8

    How can I inject a type into a function in Typescript?

  9. 9

    How can I inject a type into a function in Typescript?

  10. 10

    Typescript - How can I conditionally define a type based on another property

  11. 11

    How can I change the return type of this function?

  12. 12

    How can I specify a return type for a $http call with Typescript?

  13. 13

    In typescript, how to define type of async function

  14. 14

    Typescript: How to define overloaded function type

  15. 15

    How can I define a Rust function type which returns its own type?

  16. 16

    How can I reduce boilerplate from this function?

  17. 17

    How can I reduce boilerplate from this function?

  18. 18

    Swift: How can I make a function with a Subclass return type conform to a protocol, where a Superclass is defined as a return type?

  19. 19

    How to define a function which cannot return anything in TypeScript

  20. 20

    Typescript: Can I define an n-length tuple type?

  21. 21

    Typescript - Function return type if I want to return object

  22. 22

    Can I define a function pointer pointing to an object of std::function type?

  23. 23

    How can I return a function?

  24. 24

    How can I return a function?

  25. 25

    How can I define the LAMBDA function in LISP?

  26. 26

    How can I define a function in JavaScript?

  27. 27

    How can I define a paramter in a function.

  28. 28

    How can I write a function have a polymorphic return type based on the type argument of its type parameter?

  29. 29

    How can I type-hint a function where the return type depends on the input type of an argument?

HotTag

Archive