Questions tagged [index-signature]

34 questions
19
votes
2 answers

Difference between index signature and Record for empty object?

I cannot figure out the difference between index signatures and record types. Could someone explain the differences and when to use one versus the other? Specifically, I am looking to define the type of an object that will have random strings for…
mkusps
  • 250
  • 2
  • 7
15
votes
1 answer

How do I add an index signature for a mapped type?

Suppose I have interface interface X { a: string; b: number; c: boolean; } and a function function values(x: X) { return Object.keys(x).map(s => x[s]) } When I enable typescript's strict flag I get the error "Element implicitly has an…
Stuart
  • 197
  • 2
  • 9
11
votes
1 answer

What does a TypeScript index signature actually mean?

I've been writing TypeScript for a while and am confused about what an index signature means. For example, this code is legal: function fn(obj: { [x: string]: number }) { let n: number = obj.something; } But this code, which does basically the…
Ryan Cavanaugh
  • 164,706
  • 43
  • 239
  • 219
8
votes
4 answers

How to add index signature in TypeScript

I am trying to use reduce with Typescript to reach a total count of incoming messages. I'm confused on how to add an index signature. I keep getting the error: " Element implicitly has an 'any' type because type '{}' has no index signature." on…
Bethany
  • 653
  • 4
  • 9
  • 16
4
votes
1 answer

How to create a TypeScript interface with both an index signature and a fixed property of a different type?

From a legacy api I am getting a JSON response like so: const someObject = { "general": { "2000": 50, "4000": 100, "8000": 200, }, "foo": [ 0, 1, 2, ], "bar": [ 5, …
k0pernikus
  • 41,137
  • 49
  • 170
  • 286
4
votes
1 answer

Typescript Index Signature and methods in the implementing class doesn't work

I`m trying to create my own Typescript dictionary like class, but with some custom methods to use in my code. I can create the basic dictionary like this: export interface IDictionary { [property: string]: T; }; export class Dictionary
Wayne Barnard
  • 154
  • 1
  • 8
3
votes
1 answer

No index signature with a parameter of type 'string' was found on type 'typeof Object'

I made a class doing like enum following: https://stackoverflow.com/a/51398471 export default class Juice { [key: string]: any; static APPLE = new Juice('APPLE', 'Apple juice'); static ORANGE = new Juice('ORANGE', 'Orange juice'); private…
user2530873
  • 53
  • 1
  • 5
3
votes
1 answer

Type 'HTMLFormControlsCollection' has no property 'x' and no string index signature

The following error appears when trying to destructure a form.elements object: Type 'HTMLFormControlsCollection' has no property 'x' and no string index signature // in a class domRefs: {[key: string]: HTMLFormElement | null} = { myForm:…
Qwerty
  • 19,992
  • 16
  • 88
  • 107
2
votes
1 answer

Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'Type'

I am trying to trim the values from an array of objects that is returned from a REST API. This is the interface for the object I'm expecting. interface IProduct { productId: number; qty: number; code: string; customer: string; description:…
gmwill934
  • 378
  • 6
  • 20
2
votes
1 answer

Typescript: Using a tuple as index type

Given is a tuple of some keys like ["a", "b", "c"] and a nested object with those keys as properties {a: {b: {c: number}}}. How do you recursively use the members of the tuple as index in typescript? A implementation without proper typing: function…
MaximilianMairinger
  • 1,308
  • 1
  • 9
  • 27
2
votes
1 answer

Typescript: how can I exclude methods from the index signature of classes?

I have a class that can be extended by the user like this: class Foo { extendFoo(key: string, value: string) { this[key] = value; //<==typescript error: missing index signature } } as you can see, this can be extended with strings.…
kundasaba
  • 838
  • 2
  • 11
  • 22
2
votes
1 answer

How to iterate through controls array property from Forms in Angular

I have the following problem I need to iterate through the controls property of a Form in Angular using a forEach loop. I am writing the following code: const arr = this.bankForm.controls; arr.forEach((element: {[key: string]: AbstractControl}) =>…
Ricky
  • 1,306
  • 2
  • 20
  • 34
2
votes
1 answer

Numeric index signature typescript class, get item count or access in general

I'm currently implementing an array-like typescript class. Is there a way to count the number of items in an indexed signature object in TypeScript? The class has got member functions and properties but should be accessible via the index signature.…
Felix Lemke
  • 4,845
  • 3
  • 28
  • 54
2
votes
1 answer

How to set index signature without opening up the interface

Consider this code: interface MyInterface { foo: string bar: string baz: string } const myObj: MyInterface = { foo: "foo", bar: "bar", baz: "baz" }; Object.keys(myObj).forEach(obj => { obj = myObj[obj]; }); When enabling strict mode…
MrMamen
  • 187
  • 12
1
vote
0 answers

Strict value check of indexed types in TypeScript

I want to know if its possible to have a indexed type, where if a key is defined, the object is defined as well, but the key itself is not necessarily set. Example: type Car = { model: string }; type Cars = { [id: string]: Car}; const cars: Cars =…
Kristonitas
  • 181
  • 10
1
2 3