0

I have an array something like this:

[
  {
    id: "type_a",
    // A specific data structure for type_a here
  },
  {
    id: "type_b",
    // A specific data structure for type_b here
  },
  // ...etc
]

The order of the objects in the array is not defined and they can appear in any order.

How would I create a type in typescript to represent this structure?

So that the contents of the array only contains the defined types

SystemicPlural
  • 5,629
  • 9
  • 49
  • 74
  • Define the structures as type aliases (`type X = { id: "type_a", /*...*/};`) or interfaces (`interface X { id: "type_a"; /*...*/ }`) and define the array as an array of the union of those (`(X | Y)[]` or `Array`). – T.J. Crowder Apr 13 '22 at 10:02
  • @T How would I define that each id should only appear once? – SystemicPlural Apr 13 '22 at 10:15

0 Answers0