Questions tagged [index-signature]

Use for questions on index signatures, a feature of TypeScript that allows for object types with unknown properties (both numeric and string signatures are supported) to be restricted to certain types. Always use with the [typescript] language tag. Do not use it for questions that merely involve index signatures.

About

Index Signatures allow object types in TypeScript to have unknown properties that are restricted to certain types. Signatures can be either numeric or string-based, and both can be present at the same time (with a small caveat). The presence of an index signature also restricts known properties to be of the same type unless they are extracted in another object type and combined with the index signature via a union type.

type StringIndexed = { [key:string]: boolean; };
const validSI: StringIndexed = { isStringBased: true }; // Ok
const invalidSI: StringIndexed = { answer: 42 }; // Type 'number' is not assignable to type 'boolean'

type NumberIndexed = { [index:number]: string; };
const validNI: NumberIndexed = ["one", "two"];
const invalidNI: NumberIndexed = [1,2]; // Type 'number' is not assignable to type 'string'

type BothIndexed = {
    [key:string]: boolean|string; // Needs to be a union because A[0] is the same as A["0"]
    [index:number]: string;
};
const validBoth:BothIndexed = {
    acceptsBoth: true, // Ok
    thisIsAlsoOkThough: "anything", // a trade-off for combining signatures
    42: "answer" // Ok
}

type WithKnownProperties = {
    [prop: string]: string;
} | { answer: 42; };
const known: WithKnownProperties = {
    answer: 42, // Ok
    question: "unknown" // Ok
}

Usage Guidance

Use for questions about index signatures, their usage rules and caveats, not for questions simply involving them.

Always use with the language tag.

67 questions
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
  • 31,090
  • 47
  • 134
  • 244
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)) { …
0
votes
1 answer

Index signature with typescript

I have a problem with determining what is the correct index-signature when enabling "noImplicitAny" in the typescript. const getFromUri = () => { const urlSearch = window.location.search.substring(1); const params: { foo?: number; bar?: number }…
MrMamen
  • 359
  • 2
  • 14
0
votes
2 answers

How to avoid implicit any when iterating over object keys in Typescript

The following code: interface Foo { bar: string; qux: string; baz: number; } const foo: Foo = { bar: '42', qux: 'baz', baz: 5 }; const keysToIterateOver = [ 'bar', 'qux' ]; keysToIterateOver.forEach(key => foo[key] =…
Esa Toivola
  • 1,538
  • 3
  • 16
  • 27
0
votes
1 answer

typescript index signature accessors

I would like my javascript data to be like this: "dogs": { "ada": { age: 7, breed: poodle }, "levin": { age: 5, breed: shitzu }, ... } Where the name is an object key. I have this typescript…
HankCa
  • 9,129
  • 8
  • 62
  • 83
-1
votes
1 answer

Pivoting Data into Columns Using ES6 and Typescript and Index Signatures

have the following data structure that needs to be transformed/pivoted from: const dataReceived: IOrder[] = [ {customerName: 'Customer 1', customerId: '1',auctionName: 'Auction 1', auctionId: '1', statusName: 'Awaiting', statusId: '1',…
-1
votes
1 answer

How to properly add index signature to object

I have following errors (solution is working): Element implicitly has an 'any' type because type '{}' has no index signature. [7017] Code: const createCollection = (jsonObject: object, namesObject: object): INameHex[] => { return…
Marta
  • 2,857
  • 13
  • 46
  • 67
1 2 3 4
5