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

5
  • 1
    Which version of TypeScript are you using/compiling to? Commented May 13, 2021 at 22:32
  • 1
    I'm running typescript latest stable version, and the above code works Commented May 13, 2021 at 22:34
  • 1
    There is only 1 package which I use, it called @graphprotocol/graph-ts": "0.19.0", not sure which version they use. Let me check Commented May 13, 2021 at 22:37
  • could try this and see if it works any better. let COUNTRIES: { name: string, segments: number[] }[] = whatever Commented May 13, 2021 at 22:39
  • 1
    @Jacob the same issue Commented May 13, 2021 at 22:40

2 Answers 2

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
    ]
}]
Sign up to request clarification or add additional context in comments.

3 Comments

Receiving this issue: TS1003: Identifier expected. name: string,
I guess there should be ; instead of ,
Having different issue with this option (check question updated description)
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
    ]
}]

2 Comments

This looks very similar to the answer @Aadmaa provided considering that class works fundamentally similarly to interfaces
With interfaces it does not work for this enovirement. Not sure whats the reason

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.