Create a TypeScript interface for an object with hyphens as the property keys

I am interested in transforming

{
  "controller-element": { }
}

into an interface. However, due to the presence of a - in controller-element, I am unable to directly create

export interface IControllerResponse {
  controller-element: any;
}

Answer №1

To properly define the field name, ensure it is enclosed in quotes:

export interface IControllerResponse {
  "element-controller": any;
}


const responseData: IControllerResponse={
    "element-controller": 'example data'
}

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

Do I still need to install react-router if I have already installed @types/react-router?

My React project was initiated using create-react-app create-react-app my-app --scripts-version=react-scripts-ts Upon creation, the package.json file contained the following: { "name": "my-app", "version": "0.1.0", "private": true, "dependencies ...

Guidelines for transmitting form information to a web API using Angular

I am currently working on an Angular 6 project where I have a form and a table that retrieves data from a web API. I want to know if it's possible to send the form data to that web API. Here is the code snippet that I have so far: HTML Form: &l ...

Experiencing issues with Typescript typings.d.ts due to parsing error

Whenever I try to lint my TypeScript code, I encounter a parsing error: 2:3 error Parsing error: Only declares and type imports are allowed inside declare module 1 | declare module "*.json" { > 2 | var value: any; | ^ 3 | export defa ...

What is the best way to specify a function parameter as the `QUnit` type using TypeScript in conjunction with QUnit?

In my project, which is partially written in TypeScript and licensed under MIT, I am utilizing QUnit. I have some TypeScript functions that require QUnit as a parameter, and I would like to define their types based on its interface from the typings. For e ...

Is it possible in Typescript to restrict object creation to Record<string, T> while still being able to access the defined

Suppose I define a type called Param as follows: type Param = { field: string; active: boolean; } I then use this type to create a record with keys of type string and values of type Param, like so: const params: Record<string, Param> = { foo: { f ...

developing TypeScript classes in individual files and integrating them into Angular 2 components

We are currently putting together a new App using Angular2 and typescript. Is there a more organized method for defining all the classes and interfaces in separate files and then referencing them within angular2 components? import {Component, OnInit, Pi ...

What is the proper technique for utilizing private fields in TypeScript?

Every time I attempt to execute the code below that involves a private field, I encounter an "Invalid character" issue at the location of #. class MyClass { #x = 10; } Here is the content of my tsconfig.json file: { "compilerOptions": { ...

Every time I attempt to destructure the state object in react typescript, I encounter the error message stating 'Object is possibly undefined'

Whenever I attempt to destructure my state object in react typescript, I encounter an error stating Object is possibly 'undefined'. When I try using optional chaining, a different error pops up saying const newUser: NewUser | undefined Argument o ...

Troubleshooting problems with building an Angular project containing Angular Slickgrid

After successfully migrating my project from Angular 13 to Angular 17 and updating angular-slickgrid from version 4.1.4 to 7.5.0, I am facing a build error that I cannot resolve. I have addressed all other errors except for one: When compiling with Angular ...

Do I have to create all the classes returned when consuming a JSON web service in Angular/Typescript?

I would like to access this service: https://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&key=YOUR_API_KEY I am interested in extracting only two pieces of data: "location" : { " ...

The For loop causing crashes in the Filter button functionality

I am currently working on implementing a buy it now only filter button for listings that allow that option. However, I am facing an issue where the app crashes when the button is clicked due to a for loop in my code. Strangely, if I remove the for loop, ...

React: The Material-UI autocomplete input, controlled with the React Hook Form `<controller>` component, experiences issues when the `multiple` prop is set to `true`

Currently facing challenges with managing an autocomplete MUI component using the <controller> component in react-hook-form. Take a look at the code snippet below: <Controller control={control} name="rooms" render={({ field }) =&g ...

Fetching data from different interfaces with the same name in Angular

In my project, I have two interfaces - one is cropFilter for checkbox filtering and the other is Crop which holds the data. Let me provide you with the code for better clarity. 1. crop.model.ts export class Crop { // Interface 1 name: string; dis ...

Ways to eliminate the white background gap between pages on ionic

While developing an app using Ionic, I encountered a strange issue. Everything runs smoothly on a browser, but when testing the app on an Android 5 device, I noticed a white background appearing between pages. The app loads correctly with the custom splas ...

Update each number in an array by appending a string within a table in an Angular component, rather than the

I have created a function that decides on a comment based on the result number added to an array and displays it in a table. However, calling this function within the template is causing a performance slowdown. Is there a way to achieve the same outcome w ...

In the en-US locale, the toLocaleDateString function is transforming "00:30" into 24:30

Questioning the Conversion of Time from 00:30 to 24:30 in en-US Locale options = { year: "numeric", day: "numeric", month: "numeric", hour: '2-digit', minute: '2-digit&apo ...

Executing an individual .ts file within a Next.js application using ts-node for the purpose of testing

I'm attempting to execute a single ES module .ts file within a Next.js project using the default configuration for quick debugging: npx ts-node lib/my_module.ts However, I encounter the following error: Warning: To load an ES module, set "type&q ...

Exploring nested promises in TypeScript and Angular 2

I have a method called fallbackToLocalDBfileOrLocalStorageDB, which returns a promise and calls another method named getDBfileXHR, also returning a promise. In the code snippet provided, I am unsure whether I need to use 'resolve()' explicitly o ...

Is it possible to assign a property value to an object based on the type of another property?

In this illustrative example: enum Methods { X = 'X', Y = 'Y' } type MethodProperties = { [Methods.X]: { x: string } [Methods.Y]: { y: string } } type Approach = { [method in keyof Method ...

Ways to incorporate type into an object comprised of n element in Typescript

Is there a way to assign types to objects with different keys and values, but where "other" remains constant across all of them? How can this be achieved using TypeScript? { color: "red", size: "small", other: { price: 345 discount: 10 ...