Home > Enterprise >  Make list of map in typescript which is correspond to java List<Map<String, String>>
Make list of map in typescript which is correspond to java List<Map<String, String>>

Time:10-11

I am having a function with List<Map<String, String>> as argument type in java. I want to make a fetch call from Typescript file. For that I need to define a variable whose type should be correspond to List<Map<String, String>>.

Since typescript does not have inbuilt type as list then how can we define List<Map<String, String>> in Typescript ??

For the reference - The java function receive input as below

  [
   {
    xyz: 'Test',
    abc: 'Count',
   },
   {
    xyz: 'Test',
    abc: 'Latency',
   },
  ]

How can I receive this List<Map<String, String>> in typescript ??

CodePudding user response:

Looks like you're getting an array. Arrays can be represented with the built-in generic:

Array<T>

or using [] (similarly to Java):

T[]

The objects inside the array are normal objects, with string keys mapping to string values. This can be represented with a Record:

Record<string, string>

So you should get either:

Record<string, string>[]

or

Array<Record<string, string>>

as the type.

CodePudding user response:

I think you can simply use the type {[key: string]: string}[]. For example, this code is working on my side:

let foo: {[key: string]: string}[] = [];
foo = [
  {
   xyz: 'Test',
   abc: 'Count',
  },
  {
   xyz: 'Test',
   abc: 'Latency',
  },
 ];
console.log(foo);
  • Related