Error: Cannot assign type 'number' to type 'PersonConfig'. Please resolve the issue before continuing

An error occurred indicating that type 'number' is not assignable to type 'PersonConfig'. The issue seems to be related to the index signature in this code snippet.

interface Person {
    [Id: string]: PersonConfig
}

interface PersonConfig {
    name: string,
    age: number
}

const people: Person[] = [
    {
      "p1": {
          name: "abcd",
          age: 20
      }
    },
    {
      "p2": {
          name: "efgh",
          age: 78
      }
    }
];

The task at hand is to add a suffix to the keys of the objects in the 'people' array (specifically, "p1" and "p2").

Expected Approach:

const suffix:string = "sub";

const people: Person[] = [
    {
      "p1" + suffix: {
          name: "abcd",
          age: 20
      }
    },
    {
      "p2" + suffix: {
          name: "efgh",
          age: 78
      }
    }
];

Answer №1

Here is a solution:

let individuals: Person[] = [
    {
        ["person1" + extension]: {
            name: "Jane Doe",
            age: 30
        }
    },
    {
        ["person2" + extension]: {
            name: "John Smith",
            age: 45
        }
    }
];

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

Is there a way to prevent elements on a web page from shifting when the page width becomes narrow enough?

Currently, as I delve into the world of web development with Svelte, I am in the process of creating a basic website to put my knowledge to the test. The main aim is to ensure that no matter how much the page is resized, the text and video content remain e ...

I am interested in implementing a "slide up" effect for the "slide menu" when it loses focus

When I focus out of the asmenu class, I attempt to trigger a 'slide up' effect. However, if I select two checkboxes from the child elements within the asmenu class, the focusout event is still triggered. Ideally, I would like the 'slide up& ...

Using only Node.js, demonstrate the image

I want to show an image using only Node.js without involving HTML or ID's. I have been looking for solutions but most examples I find use HTML, which I prefer not to use. Unfortunately, I don't have any code to share, but I'm wondering if th ...

I successfully managed to ensure that the splash screen is only displayed upon the initial page load. However, I am now encountering an issue where it fails to load again after I close the page. Is there any possible solution

After successfully implementing a splash screen that only plays once on page load, I encountered an issue. When I close the tab and revisit the page, the splash screen no longer plays. Is there a way to fix this problem? Perhaps by setting a time limit for ...

How can the printing of content be adjusted when the browser zoom function is activated?

Is there a way to prevent the content from zooming when printing while the browser is zoomed in? The goal is for the printing (using iframe) to remain unchanged even if the browser is zoomed. I attempted: document.body.style.transformOrigin = 'top le ...

Clicking does not result in a change of the view

While experimenting with this code snippet : http://jsfiddle.net/9fR23/187/ Something seems off as the table elements are not being hidden when clicking on the "Flip!" div. The elements are supposed to be hidden based on the state change determined by th ...

express-validator never accepts valid input

Currently, I am working on a project using the most recent version of nodejs and express. The basic site setup is complete, and now I am focusing on implementing user authentication based on what I've learned from this course. However, no matter what ...

Is there a way to easily uncheck all MaterialUI Toggle components with the click of a button or triggering an outside

If you have a set of <Toggle /> components in an app to filter clothes by size, and you want to reset all the filters with a single click on a button rather than unchecking each toggle individually. Is it possible to achieve this using materials-ui ...

What is the best way to troubleshoot the TypeScript error I am encountering in my JavaScript file?

Currently experiencing a TypeScript error within a JavaScript file https://i.sstatic.net/gBzWx.png The issue is within a folder containing only one JavaScript file, and there are no Node.js or package.json files present. I have disabled the TypeScript ex ...

Generate a fresh array using a current array comprising of objects

Greetings! I am facing a challenge with an array of objects like the one shown below: const data = [ {day: "Monday", to: "12.00", from: "15.00"}, {day: "Monday", to: "18.00", from: "22.00"}, {day: ...

What is the correct way to define the onClick event in a React component?

I have been encountering an issue while trying to implement an onClick event in React using tsx. The flexbox and button are being correctly displayed, but I am facing a problem with the onClick event in vscode. I have tried several ideas from the stack com ...

Attempting to retrieve XML/JSON

I am struggling with extracting the first 15 words from a file using the API. I have attempted to do it with both XML and JSON, but keep encountering this error: XMLHttpRequest cannot load No 'Access-Control-Allow-Origin' header is present on th ...

Enhancing planetary textures with high resolution using Three.js technique

Creating realistic textures for planets has proven to be quite challenging. Despite using a 4096k image wrapped around a high poly sphere, there are performance issues due to the large file size and the texture appearing pixelated at close range. One solu ...

Have they developed a polygon amoy chain specifically for thirdweb?

I'm currently attempting to utilize the polygon amoy testnet on a local server. Unfortunately, I haven't been able to determine if there's a method for accessing this chain via thirdweb. My code is implemented in typescript. Although I att ...

Transforming JSON into object instances with Angular

I am facing an issue in my Angular application where I need to convert a JSON object into an array. Although the mapping process is successful, the data within the array does not retain the properties and methods of my original object class. This hinders m ...

Tips for accessing a value in a multidimensional json array without the key in JavaScript/jQuery?

I'm working with a JSON file that contains a multidimensional array. The array consists of city data at the first level and temperature data at the second level. I'm facing difficulties in dynamically extracting values from the second level. I a ...

Is there a way to determine whether the uploaded file has been altered?

Is there a way for me to verify if a file uploaded by the user to my server has been altered since their last upload? My database includes a log table containing User_id and FileName (each User_id is unique). Once I have read the contents of the file, I d ...

In React Native, changing the translation of an element causes it to shift below all other elements, regardless of

Check out this sandbox project: I'm trying to create a simple animation using translation in React Native, but I'm facing an issue where when I move the element to the right and down, it goes under other elements. However, if I move it left and ...

Angular.js is a great tool for showing PDF files on a

I am facing a challenge while trying to incorporate a PDF file into my Angular.js website. The PDF is stored as a blob in a MySQL database and I want to display it as one of the partial views that appear on the main page when a link is clicked. Unfortuna ...

What are the steps to modify the sign in page on keystone.js?

I recently started using keystone.js and I'm having trouble locating the sign in page within the files. I've searched through all of the keystone.js files but can't seem to find where it's written. Can someone please guide me on how to ...