Search for the enumeration type of the given string does not have an assigned index signature

I am working with a string enum where each value is associated with a display name as shown below:

enum MyEnum {
  key1 = 'one',
  key2 = 'two',
  key3 = 'three',
}

const myKey: MyEnum = 'two' as MyEnum; // The value for myKey is only known at runtime

const lookupResult = {
  [MyEnum.key1]: 'display name for key1',
  [MyEnum.key2]: 'display name for key2',
}[myKey];

However, I encountered an error when using the lookup object:

TS7017: Element implicitly has an 'any' type because type '{ [MyEnum.key1]: string; [MyEnum.key2]: string; }' has no index signature.

How can I successfully match myKey to its display name without triggering this error?

Do you think it would be better not to use an enum in this scenario? I require both the enum values and their corresponding display names for different contexts.

Answer №1

Utilize the enum key for easy referencing.

enum MyEnum {
  key1 = 'one',
  key2 = 'two',
  key3 = 'three',
}

const lookupResult = {
  [MyEnum.key1]: 'display name for key1',
  [MyEnum.key2]: 'display name for key2',
}[MyEnum.key2];

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

Material-UI Slide component is encountering an issue where it is unable to access the style property of an undefined variable

Recently, I incorporated Material-UI Slide into my project and I'm curious about why the code functions correctly when written in this manner: {selectedItem && selectedItem.modal && selectedItem.modal.body ? ( selectedItem.modal.body.map((section ...

Component declaration in Typescript is being rejected due to the union type not being accepted

In my current project, I'm working on a component that should accept either an onClick or a to prop. const StickyButton: React.FC< ({ onClick: MouseEventHandler } | { to: string }) & { buttonComponent?: ({ onClick: MouseEventHandler }) =& ...

Angular not triggering event upon changing URL fragment subscription

I'm currently using Angular 13 and attempting to subscribe to the ActivatedRoute 'fragment'. However, I am facing an issue where the subscription only receives a notification when the page initially loads, and does not update when the fragme ...

Can TypeScript restrict a callback parameter based on the type of another parameter using generics?

I am currently developing an event manager system. The main objective is to allow users to subscribe to events by providing an event type and a callback function. In my implementation, events are represented as classes, where AwesomeEventType in the exampl ...

What is the method for launching a standalone terminal window from a vscode extension?

I am in the process of creating a custom extension for Visual Studio Code. My goal is to open a separate terminal window and execute multiple commands consecutively, similar to Terminal.sendText but not within the integrated terminal. Is there a method to ...

Struggling with importing aliases in TypeScript for shadcn-ui library

I am facing a challenge with resolving TypeScript path aliases in my project. I have set up the tsconfig.json file to include path aliases using the "baseUrl" and "paths" configurations, but alias imports are not functioning as intended. My goal is to imp ...

Issues encountered while attempting to utilize the delete function within a CRUD process involving Angular 8, Laravel, and JWT authentication

Currently, I am developing a Single Page Application using Angular 8 on the frontend and Laravel on the backend. The frontend consists of a form with options for users to either edit or delete a user. When the delete button is clicked, I capture the curren ...

Tips for Structuring Code in a Resource Management Phaser Typescript Project

I'm currently developing a resource-management game and require a "collection manager" to streamline interactions between states and objects in Typescript. Let's imagine the game revolves around nurturing cats. In one state, players advance time ...

send the checkbox control's model value back to the parent control in Vue3

I've implemented a wrapper control for checkboxes that closely resembles my textbox control. This approach ensures consistent validation and design throughout the application. While I was successful in getting it to work for textboxes, I encountered s ...

Check out the computed typescript types

In my work with TypeScript types, I find myself frequently using Omit, Pick, and similar tools based on other types. While it generally gets the job done, I often struggle with readability when dealing with complex type manipulations. I am interested in f ...

Update TypeScript definitions in version 2.2.2 obtained from NPM @Types

I am currently utilizing the component react-router-bootstrap along with the definitions from DefinitelyTyped. However, the downloaded definitions do not align with the component. While I have submitted a pull request to rectify this issue, it has not yet ...

Angular encountered an error when trying to access the property "fruits" of an undefined object

When working with Angular, I encountered an issue where I received the error message "cannot read property 'fruits' of undefined." Within my class definition, I have included the following: export class MyClass implements OnInit { fruits: any[] ...

Immutable.Map<K, T> used as Object in Typescript

While refactoring some TypeScript code, I encountered an issue that has me feeling a bit stuck. I'm curious about how the "as" keyword converts a Map<number, Trip> into a "Trip" object in the code snippet below. If it's not doing that, the ...

What is the process for incorporating a restriction in a map within a generic framework?

Note: The explanation provided was not clear enough; you might find it helpful to move on to the next example. I'm facing an issue with the constraints on generics and struggling to identify my mistake. Here is the code snippet: Code snippet goe ...

What is the method for generating a data type from an array of strings using TypeScript?

Is there a more efficient way to create a TypeScript type based on an array of strings without duplicating values in an Enum declaration? I am using version 2.6.2 and have a long array of colors that I want to convert into a type. Here is what I envision: ...

Embarking on a fresh XR experience

As a newcomer to TypeScript, I am exploring how to create a functionality similar to a "double-click" event for a hand-input controller in my Three.js application. Here is my approach: - I monitor a click event - I check the timing between the last click ...

Exporting External JavaScript Variables in Angular 2 Using Typescript

In my Configuration JS file, I have defined some configuration variables. For example: var config = {url:"xyz.com"}; I need to access these configuration parameters throughout my application. I attempted to export the config variables like this: export ...

Mapping data using MapState is a useful technique that can help organize and access data

Is it possible to use ...mapstate to fetch data from the store instead of directly accessing it like this.$store.state.workermanage.staff.staff? Any suggestions are appreciated, thank you. persons: this.$store.state.workermanage.staff.staff ...

Counting up in Angular from a starting number of seconds on a timer

Is there a way to create a countup timer in Angular starting from a specific number of seconds? Also, I would like the format to be displayed as hh:mm:ss if possible. I attempted to accomplish this by utilizing the getAlarmDuration function within the tem ...

Typescript error code TS7053 occurs when an element is detected to have an implicit 'any' type due to an expression of a different type

I encountered an issue with the provided example. I'm uncertain about how to resolve it. Your assistance would be greatly appreciated. type TestValue = { value: string; }; type FirstTest = { type: 'text'; text: TestValue[]; }; typ ...