2

I am trying to create an array of objects for my project, but during the build, I face this issue:

TS1110: Type expected
TS1109: Expression expected

error screenshot

My array looks like this:

export let COUNTRIES: Array<{ name: string, segments: number[] }> = Array({
   "name":"Afghanistan",
   "segments":[
      4687,
      4787,
      4790,
      4795,
      4880
   ]},{
   "name":"Albania",
   "segments":[
      4136,
      4248
   ]})

I tried this solution from Stack Overflow but to no results.


Tried with an interface, having another issue:

error screenshot

2 Answers2

1

I think you might mean this?

interface Country {
    name: string;
    segments: number[];
}

export let COUNTRIES: Country[] = [
    {
    "name":"Afghanistan",
    "segments":[
       4687,
       4787,
       4790,
       4795,
       4880
    ]},{
    "name":"Albania",
    "segments":[
       4136,
       4248
    ]
}]
Aadmaa
  • 829
  • 5
  • 13
0

If someone interested here is the final solution:

export class Country {
  name: string
  segments: number[]
}

export let COUNTRIES: Country[] = [
    {
    "name":"Afghanistan",
    "segments":[
       4687,
       4787,
       4790,
       4795,
       4880
    ]},{
    "name":"Albania",
    "segments":[
       4136,
       4248
    ]
}]
  • This looks very similar to the answer @Aadmaa provided considering that `class` works fundamentally similarly to `interfaces` – Jacob May 16 '21 at 19:16
  • 1
    With interfaces it does not work for this enovirement. Not sure whats the reason –  May 16 '21 at 23:10