Questions tagged [index-signature]

34 questions
1
vote
2 answers

Enum index signatures in TypeScript/React

I can't seem to figure out how to type the index signature properly here. I have an enum and need to iterate through it to put some JSX on the screen. I can guess what it's telling me but I can't solve it in my code. Both the Category[el] statements…
1
vote
1 answer

How to type object with index signature and dynamic variable in TypeScript?

I am new to TypeScript, and have tried various ways to type this, but running into problems with index signatures. What should the interface look like? interface MyConfig { ... } // someVar can be any string let someVar = "dynamicKey"; // the…
phifa
  • 498
  • 3
  • 9
1
vote
1 answer

Typescript: How to reference property name in an string index signature definition in an interface

Illustration export interface IEventDef { ready: any, } export interface IIPCMessage { eventType: keyof IEventDef, data: IEventDef[this['eventType']] } interface IMessageHandlers { [eventName: keyof IEventDef]: (data: ) =>…
Mohamed Allal
  • 9,980
  • 2
  • 58
  • 61
1
vote
2 answers

Typing a single Record entry

I'm looking for a TypeScript type definition that describes an object with a single property (having any value). I know that thare are index signatures, e.g. type X = { [key: string]: any } or alternatively type X = Record However…
scythe
  • 11
  • 1
1
vote
1 answer

Typescript: add typing for response object containing both index signature and key-pairs

I am unsure of the best way to add typescript typings for this response object I am receiving from a backend service: { de49e137f2423457985ec6794536cd3c: { productId: 'de49e137f2423457985ec6794536cd3c', title: 'item 1', }, …
AshJapan
  • 13
  • 4
1
vote
1 answer

Can I use an index signature to create an array of descriptions for an enum?

Suppose I have the following enum: export enum ApiRole { User = 1, SuperUser = 2, Restricted = 3, } Is there a way for me to easily create an array that I can use these enum values to index that will return a string value I can use as a…
ldam
  • 3,971
  • 5
  • 38
  • 67
1
vote
2 answers

Can I instantiate TypeScript classes via proxy?

Note: I have updated the question to hopefully make things clearer. I am trying to instantiate classes through a proxy object called elemTypes. This object is a list of references to classes that I want to use to programmatically build objects based…
f1lt3r
  • 1,878
  • 18
  • 24
0
votes
2 answers

Strongly typing Twitter widget to get rid of member access errors

I'm having difficulty strongly typing my Twitter widget. At the moment, it throws me a bunch of errors including: ESLint: Unsafe call of an any typed value.(@typescript-eslint/no-unsafe-call) ESLint: Unsafe member access .catch on an any…
methuselah
  • 11,066
  • 40
  • 134
  • 242
0
votes
2 answers

TS: `unions can't be used in index signatures, use a mapped object type instead` Then mapped object is not passed as string/number

After getting the error unions can't be used in index signatures, use a mapped object type instead I am now attempting to convert a string literal union (key names of an interface) to a mapped object, as explained here.…
Sean D
  • 2,172
  • 4
  • 23
  • 54
0
votes
1 answer

Optional chaining for interfaces that have an index signature

I defined two interfaces. The first one has an optional field, the second one has an index signature: interface A { foo?: { bar: number }; } interface B { [s: string]: { bar: number }; } Why does the first interface give me a result of type…
0
votes
2 answers

Index signature for iteration in typescript

We can all see that this code is valid javascript: const myObj = { foo: "string", bar: 123, baz: "important stuff" }; ['foo', 'bar'].forEach((key) => { delete myObj[key]; }); Element implicitly has an 'any' type because expression of type…
MrMamen
  • 187
  • 12
0
votes
0 answers

How to restrict keys of Typescript index signatures

I have an object literal that maps key-value pairs. Is it possible to restrict the index signature key to a range of pre-defined values? I tried the following but no error gets thrown when using the unspecified key "c": let foo: { [key in 'a' |…
KayO
  • 179
  • 2
  • 13
0
votes
1 answer

Extending an interface with index signature in generic while having typing for implementations

I have a system that takes key:object pairs and then lets you get them later in the application. The problem is having this index signature on breaks the typing because it allows any key at all to exist so you can't type the get() requests. A…
sailingonsound
  • 103
  • 1
  • 7
0
votes
1 answer

index signature of a key in an object

i have the following typescript code type MapOfErrors = Map interface GatheredErrors { 'dev': MapOfErrors 'prod': MapOfErrors [key: string]: MapOfErrors } const errors: GatheredErrors = { dev: new Map
Nick Ginanto
  • 26,414
  • 39
  • 123
  • 214
0
votes
1 answer

How do I access the properties of a generic object by index in TypeScript?

I have the following function that goes through all the attributes of an object and converts them from ISO strings to Dates: function findAndConvertDates(objectWithStringDates: T): T { for (let key in Object.keys(objectWithStringDates)) { …