"What is the correct way to utilize a dash in a Typescript declaration file when working with JSON

Once I've imported a JSON file into my Typescript application, I utilize an interface to enable code completion in my IDE for the JSON data:

interface Component {
    name:string
}

This method works perfectly fine, however, I encountered a problem when dealing with a property named en-US within the JSON. Since the dash is not allowed in an interface, I'm unsure how to resolve this issue?

{
    "name" : "boink",
    "en-US" : "hello there"
}

Answer №1

To specify attributes within your interface, enclose them in quotes:

interface Component {
    'en-US': string;
}

Keep in mind that you will need to reference the property using quotes every time you want to access it:

 let myComponent: Component = {
     'en-US': 'hello there'
 }
 let translation = myComponent['en-US'];

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

Adding new data to a JSON file without overwriting the entire file

I am working with a JSON file that has one of its attributes stored as an array. My goal is to continuously add new values to this array and save it back to the file. Is there a method to avoid overwriting existing data and simply append the new values ins ...

Guide on transforming Div content to json format with the use of jquery

I am trying to figure out how to call the div id "con" when the export button is clicked in my HTML code. I want it to display JSON data in the console. If anyone has any suggestions or solutions, please help! <html> <div id ="con"> < ...

What is the best way to clear a form in a Next.js 13.4 component following a server action?

Currently, I am working on a component using next.js 13.4, typescript, and resend functionality. My code is functioning properly without clearing data from inputs, as it uses the "action" attribute which is commented out. However, I started incorporating ...

Is it possible to implement a customized pathway for the functions within an Azure function app?

Recently, I set up a new function app on Azure using Azure Functions Core Tools with Typescript as the language. The app includes a test function named MyTestFunction that responds with an HTTP response when called. This particular function is located in ...

typescript error: Unable to access properties of an undefined value

I am facing an issue while trying to import a class in my TypeScript code. I have tried using private classA = new ClassA, but it's not working as expected and the result is undefined. Here is my code: import JWT from "../Utils/JWT" import { ...

Could the absence of a tree view in the div be due to an error in the positioning of the DOM

I'm currently working on displaying a tree structure with specific child elements inside a div using JavaScript and HTML. Despite creating all the necessary objects and ensuring that the data is correctly read from the JSON file (refer to the "data" i ...

Is it better to use project remote images instead of local ones?

I am currently experimenting with implementing the MosaicUI project from here into my own project. One issue I am encountering is figuring out how to transition from loading local images stored in a json file to fetching images from my web server. Has any ...

What is the best way to send a JSON object containing special characters to a servlet?

Is there a way to pass JSON values to the servlet in order to handle the request smoothly? On the client side, I have encountered an issue where special characters like "&" are causing problems. The input provided is as follows: "<h3>Welcome t ...

The detection of my query parameters is not working as expected

Creating an Angular application that dynamically loads a different login page based on the "groupId" set in the URL is my current challenge. The approach involves sending each client a unique URL containing a specific "groupId" parameter. A template is the ...

Login System Encounters Syntax Error Due to JSON Input Ending Abruptly

I'm currently working on implementing a login system, but I've run into an issue with unexpected end of JSON input. It's a bit confusing since the code works fine on another page. The error specifically points to line 12 in my JavaScript. H ...

What is the best way to select types conditionally based on the presence of a property in another type?

To begin with, I have a specific desired outcome: type Wrapper<ID extends string> = { id: ID }; type WrapperWithPayload<ID extends string, Payload> = { id: ID, payload: Payload }; enum IDs { FOO = "ID Foo", BAR = "ID Bar", BAZ = "ID Baz ...

Steps to fix: "Rule '@typescript-eslint/consistent-type-assertions' is missing a definition"

My React app is failing to compile because it can't find the rule definition for '@typescript-eslint/consistent-type-assertions'. I'm feeling quite lost at the moment. I can't seem to locate any current rule definitions within the ...

Performing actions simultaneously with Angular 2 directives

My custom directive is designed to prevent a double click on the submit button: import { Directive, Component, OnInit, AfterViewInit, OnChanges, SimpleChanges, HostListener, ElementRef, Input, HostBinding } from '@angular/core'; @Directive({ ...

Error in Flutter: Casting 'List<dynamic>' to 'Map<String, dynamic>' is not a valid subtype

Currently attempting to convert a list into JSON format for Firestore, the code snippet below is what I have: static Map<String, dynamic> toJson(Message? message) => { 'id': message?.id, 'senderId': message?.s ...

Latest JSON toolkit for fetching information from URLs

Currently, I am on the hunt for a reliable and updated JSON file that can successfully extract information from a link like this one: https://api.coinmarketcap.com/v1/ticker/bitcoin/?convert=CAD By clicking on the above link, you will be able to view the ...

Transform an array of Boolean values into a string array containing only the values that are true

Suppose we have an object like the following: likedFoods:{ pizza:true, pasta:false, steak:true, salad:false } We want to filter out the false values and convert it into a string array as shown below: compiledLikedFoods = ["pizza", "steak"] Is t ...

Is there a way to disable or reassign the ctrl + left click shortcut in Visual Studio Code?

Is there a way to disable or change the Ctrl + left click function in Visual Studio Code? I find that I accidentally trigger it about 20% of the time when selecting text with my mouse, and it interrupts my workflow. Any suggestions on how to fix this issue ...

Python Error: "Cannot find method __enter__"

I seem to be having trouble loading my json file and I'm at a loss for why. Can someone shed some light on what might be the issue? async def process(msg, arg): if arg[0] == prefix + "dailycase": with open("commands/databases/case ...

How can one efficiently retrieve deeply nested JSON data on iOS using Swift?

Working with Swift 5.1, I am faced with handling a complex JSON file that is deeply nested. My main goal is to extract specific elements from the JSON data while disregarding the rest of it. To illustrate, I am specifically interested in retrieving the v ...

Angular micro front-end powered by module federation

I am interested in developing micro front-end applications using module federation. I have successfully implemented it following the guidelines provided on this informative page. However, I am facing a challenge where I want each project to have its own u ...