Having trouble initialising an array of objects in TypeScript? (TS1109: Expression expected)

While working on my project, I encountered a problem when trying to create an array of objects:

Error TS1110: Type expected
Error TS1109: Expression expected

https://i.sstatic.net/Y5qb8.png

This is how my array is structured:

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

I attempted to use this solution from Stack Overflow but it did not work for me.


Also tried using an interface, but encountered another issue:

https://i.sstatic.net/3MT8a.png

Answer №1

Could this be what you're looking for?

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

export let COUNTRIES: Country[] = [
    {
    "name":"Afghanistan",
    "segments":[
       4687,
       4787,
       4790,
       4795,
       4880
    ]},{
    "name":"Albania",
    "segments":[
       4136,
       4248
    ]
}]

Answer №2

If you're curious, here is the solution that was ultimately found:

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

export let COUNTRIES: Country[] = [
    {
    "name":"Afghanistan",
    "segments":[
       4687,
       4787,
       4790,
       4795,
       4880
    ]},{
    "name":"Albania",
    "segments":[
       4136,
       4248
    ]
}]

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Mastering the art of duplicating an array of objects in TypeScript

I attempted the following strategy: this.strategies = []; this.strategiesCopy = [...this.strategies]; Unfortunately, it appears this method is not effective as it results in duplicates. ...

Encountering difficulty when trying to define the onComplete function in Conf.ts. A type error is occurring, stating that '(passed: any) => void' is not compatible with type '() => void'.ts(2322)'

I have been developing a custom Protractor - browserstack framework from the ground up. While implementing the onComplete function as outlined on the official site in conf.ts - // Code snippet to update test status on BrowserStack based on test assertion ...

Using jQuery to manipulate input arrays that are dynamically generated

My form is dynamic and consists of the following fields: <tr> <td><input name = "qty[]" /></td> <td><input name = "color[]" /></td> <td><input name = "price[]" /></td> <td><input nam ...

Converting Typescript fat arrow syntax to regular Javascript syntax

I recently started learning typescript and I'm having trouble understanding the => arrow function. Could someone clarify the meaning of this JavaScript code snippet for me: this.dropDownFilter = values => values.filter(option => option.value ...

Issues with Typegoose and Mongoose Enums when utilizing an array of strings

One of my enums is defined as follows: export enum Careers { WEB_DEVELOPMENT = 'Web Development', MOBILE_DEVELOPMENT = 'Mobile Development', UI_UX = 'UI/UX' } This particular enum is used as a mongoose property like so: ...

Issue with Angular Material date picker: Date Parsing UTC causing dates to display as one day earlier

After exploring numerous threads related to this issue and spending several days trying to find a solution, I may have stumbled upon a potential fix. However, the workaround feels too messy for my liking. Similar to other users, I am encountering an issue ...

Embrace the use of square brackets when formatting JSON output

Below is the current output that I am getting: {"name":"a","path":"a","type":"folder","items":{"name":"b","path":"a/b","type":"folder","items":{"name":"c.docx","path":"a/b/c.docx","type":"file","size":"20"}}} I want to modify it to add brackets in the "i ...

The glitch in VueJS's array updating functionality

I am facing an issue where I have an array called tiles with a title. On creating the instance, I add a field to this array named active. Subsequently, I display this array within an <li> element and iterate through it to display the title and activ ...

Using Typescript to set a custom timeout duration based on a dynamic variable within a for loop

My function includes a timeout that changes every 3 seconds: setActiveImage(promotions) { for (let i = 0; i <= promotions.length - 1; i++) { setTimeout(()=> { this.activeImage = 'http://myrul/public/Commercials/' + promo ...

Guide to Reverting the Two-Way ngModel Binding Data in Angular 2

I am utilizing a form in angular 2 that includes two-way binding data value ([(ngModel)]) to enable both edit and add functionality. When a user selects the edit option on the listing page and modifies the input, the new values automatically appear on the ...

When you find that the plugins on pub.dev do not offer web support, consider utilizing npm packages for Flutter web development

I am currently working on developing a cross-platform app using Flutter for iOS, Android, and web. However, some plugins do not support web. Fortunately, I came across npm packages that provide the same functionality and I am considering integrating them. ...

Incorporating an Angular 2 Directive within the body tag of an

My goal is to create a custom directive that can dynamically add or remove a class from the body element in HTML. The directive needs to be controlled by a service, as I want to manage the visibility of the class from various components. Question: How ca ...

What is the best way to utilize typed variables as types with identical names in Typescript?

Utilizing THREE.js with Typescript allows you to use identical names for types and code. For instance: import * as THREE from '/build/three.module.js' // The following line employs THREE.Scene as type and code const scene: THREE.Scene = new THRE ...

Tips on converting Nextjs generated Prisma types case from snake_case to camelCase

I have a full-stack application built with Next.js and Prisma ORM "next": "12.3.0" "prisma": "^4.5.0" Essentially, I am looking to convert the case of my types from snake_case to camelCase to align with the front-en ...

How should I proceed if a TypeScript definition file that I am relying on is lacking a specific definition?

I have encountered an issue while using the React type definitions for my project. The focus method is missing on elements in the array returned by the refs property, which prevents me from getting a specific example to work. The compiler error states: pro ...

How can we pass the onClick prop from a child component to a parent component in React with Typescript?

Currently, I am utilizing React along with TypeScript. I am curious about the process of passing an event from the parent component to a child component using props. Here is an example for better understanding: parent.tsx const ParentComponent: React.F ...

The Vue CLI project, using Typescript, is facing challenges with building and running Mocha tests

My Vue 2 project, created using Vue CLi, is encountering numerous errors. While it compiles fine for development purposes, running unit tests or building for production results in a cascade of issues. Displayed below are some sample errors, along with sni ...

Discovering the most efficient method for locating elements within a 2D cell array

Consider the following 2D cell array structure: my_cells= Columns 1 through 11 {1x6 cell} {1x8 cell} {1x2 cell} {1x7 cell} {1x7 cell} {1x6 cell} {1x7 cell} {1x7 cell} {1x8 cell} {1x5 cell} {1x7 cell} Columns 12 through 22 ...

Guide to obtaining specific top elements from an array using JavaScript

I'm seeking assistance with sorting arrays in JavaScript. Here is an example of a sorted array: mainArray : [25 20 20 20 18 17 17 15 12 12 10 5 5 ] The mainArray may contain duplicate values. A. Dealing with duplicates Based on user input, I need ...

Angular 5 - Jasmine Tests explained: Encounter with the puzzling error message: "Error: Provider for the NgModule 'DynamicTestModule' is invalid, as only instances of Provider and Type are permitted"

I'm having trouble running tests on a component class. Here's the error message from the stack: Error: Invalid provider for the NgModule 'DynamicTestModule' - only instances of Provider and Type are allowed, got: [AlertModaldataCompon ...