Using TypeScript's interfaces to push items

Can anyone provide some guidance on working with interfaces in typescript?

I currently have the following 3 interfaces:

export interface HomeMenu {
    [name: string]: MenuItem;
}

export interface MenuItem {
    title: string;
    route: string;
    homeMenu?: HomeMenu;
}

export interface Menu {
    homeMenu: HomeMenu;
}

Here is what I am trying to do:

var json: Menu = {
    "homeMenu": {
        "aname1": {
            "title": "text",
            "route": "myroute"
        },
        "aname2": {
            "title": "text",
            "route": "myroute",
            "homeMenu": {
                "aname21": {
                    "title": "text",
                    "route": "myroute"
                },
                "aname22": {
                    "title": "text",
                    "route": "myroute"
                }
            }
        },
        "aname3": {
            "title": "text",
            "route": "myroute"
        }
    }
}
json.homeMenu["aname2"].title = "myTitle";

However, I am having trouble adding a new item to the HomeMenu. I attempted to extend the HomeMenu interfaces with Array but received an error regarding index signature requirements. Arrays expect numerical indices, not strings
I have searched through Typescript documentation and various forums without finding a solution. Is there another way to add a new item to the homeMenu object? Any help would be greatly appreciated. Thank you.

Answer №1

What about this:

json.navMenu["item3"] = {
    name: "Item Three",
    link: "link3"
};

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

Alternative for Jquery: a new Hash Table solution using Prototype JS

Greetings everyone! I have experience as a prototype JS developer but now I am transitioning to jQuery for work/client reasons. One feature I really liked in Prototype was the Hash class, such as var h = new Hash();. However, I understand that jQuery doe ...

Header on top of table that stays in place with scrolling

I'm facing an issue with a table that contains user-generated data. We are utilizing a plugin known as Clusterize, which allows us to smoothly scroll through large amounts of rows. However, the specific markup required for this plugin seems to be caus ...

How can I asynchronously parse JSON data from a URL on a Windows phone and display it in a

As an Android developer exploring the world of Windows Phone for the first time, I came across this resource on how to handle list boxes in Windows Phone 7/8. However, my challenge now is parsing JSON from a URL instead of XML as shown in the example. Whil ...

When the result of Ajax is identical to that obtained without Ajax, the innerHTML remains the same

I understand that utilizing innerhtml is generally considered a poor practice due to the potential for XSS vulnerabilities (). However, consider the scenario below: I have a webpage generated through a Twig template called index.html.twig. When using te ...

The term 'string' is typically employed as a data type, yet in this instance it is being utilized as an actual value

Just started working with TypeScript and encountered an issue while trying to set the state. Encountered this error message: 'string' is a type and cannot be used as a value here. const state = reactive({ user: { uid: "", ...

Is it possible for me to perform cross-site scripting using JavaScript within my personal browser, specifically Firefox?

While watching a Video Tutorial on a certain website, I noticed that the videos were hosted on platform.thinkific.com and displayed in a light theme. However, I prefer Dark Theme, so I decided to invert the color of the video using the filter: invert(90%) ...

"Track the upload progress of your file with a visual progress

I have written this code for uploading files using Ajax and PHP, and I am looking to incorporate a progress bar to indicate the percentage of the upload. Here is the code snippet: <script> $("form#data").submit(function(){ var formData = new ...

What is the procedure for manipulating a string within an array of objects with a string key-value pair using underscore.js?

I am seeking a solution utilizing underscores, but I am open to a vanilla JS alternative if it proves to be the most effective option. My goal is to utilize array 2 to modify strings in the objects of array1 that either start with or end with the strings ...

When the enter key is pressed, scope.$watch() does not trigger, and location.path does not function as expected

I've encountered a peculiar problem with AngularJS that I need help with. Here's the situation: Using scope.watch to monitor text changes in a text field (searchloco). <input class="typeahead" ng-model="searchloco" data="{{varu}}" search-ba ...

The subscriber continues to run repeatedly, even after we have removed its subscription

The file brief-component.ts contains the following code where the drawer service is being called: this.assignDrawerService.showDrawer(parameter); In the file drawer-service.ts: private drawerSubject = new BehaviorSubject<boolean>(false); public ...

Simple steps for calling a lit component in React using the ScopedElementsMixin:1. Import

Looking to incorporate the web component Button (lit) into my project using a similar tag approach. For instance, if the <button-test> tag is present on the website, then it should be converted to <button-test-12345>. This is why ScopedElements ...

Is it possible to convert a .gif file into a jpeg image format for mobile display on my Gatsby website?

I'm looking to convert a .gif file into a mobile.png image for my Gatsby site, but I'm struggling to find the right resources. Does anyone have suggestions on how I can achieve this? Here is the frame: https://i.sstatic.net/IjYv4.png ...

React and Enzyme are coming up empty-handed when trying to locate any elements within a functional component

After creating a simple functional component in React with Typescript, I encountered an issue while testing it. Every time I try to gather the divs, I keep receiving an empty object {}. Here is how the component is structured: export const TestComponent ...

Exploring React's State Management with useState

Having some trouble with my introductory React Use State practice, specifically when trying to implement a counter and a button for generating random user numbers Check out the code below: import React, { useState } from 'react' import './A ...

What is the best method for adding files to JSZip from a remote URL?

Is it possible to load files into a Zip folder from a specified URL? For example: var zip = new JSZip(); zip.file("file.txt", "/site.net/files/file.txt"); Update I am following this example: I attempted the code provided but it was unsuccessful. I do ...

How can one determine the completion of a chunked download request in Angular's HTTP client?

Currently, I am utilizing angular's HttpClient to retrieve an arraybuffer. The server is transmitting the data along with the following headers: *To avoid any confusion, the download route essentially retrieves a chunk file stored in the cloud. Howev ...

Triggering a click event on various instances of a similar element type using the onclick function

Hey there, I'm a newcomer to Javascript. I've been practicing my Javascript skills and trying to replicate something similar to what was achieved in this example: Change Button color onClick My goal is to have this functionality work for multip ...

The application runs smoothly during development, but encounters issues once deployed on Heroku

I am encountering an issue while deploying my app on Heroku. The deployment process goes smoothly, but when I try to open the app, I receive the following error message: Application error An error occurred in the application and your page could not be ser ...

Preserve the authentic picture along with a blur mask that can be dragged and applied to it

Is there a way to preserve the original image while having a draggable blur mask over it? If you want to see an example of a draggable blur mask over an image, you can check out this link: https://codepen.io/netsi1964/pen/AXRabW $(function() { $("#ma ...

Interacting with Apexcharts: Exploring a Tiny Column with Hover

While using apexcharts, I encountered an issue with small columns. It can be quite challenging to render the tooltip properly because you have to hover very precisely over the column. Query: Is there a way to expand the hover area to make it easier to int ...