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
1
vote
1 answer

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
  • 201
  • 1
  • 10
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…
Florian Kuc
  • 127
  • 7
1
vote
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
  • 3,810
  • 11
  • 45
  • 90
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
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
  • 189
  • 2
  • 13
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
  • 4,412
  • 6
  • 45
  • 76
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
  • 2,176
  • 22
  • 26
0
votes
1 answer

Type ... is missing the following properties from type ... assigning key and value in empty object using index signature faced error

I am trying to create it where the object keys are unknown in the beginning (util I got testArray from the previous page). { a: [ { key1: 0, key2: 0 }, { key1: 0, key2: 0 }, { key1: 0, key2: 0 } ], b: [ { key1: 0, key2: 0 }, { key1: 0, key2: 0…
TungTung
  • 163
  • 7
0
votes
1 answer

How can I restrict fields in an interface to match the fields of a generic parameter?

I would like to define an interface (or other type?) that describes another object (whose type should be provided as a generic parameter). Basically, it should look something like this: interface IDescriptor { propSet1?: (keyof…
F-H
  • 663
  • 1
  • 10
  • 21
0
votes
1 answer

Create model in C# for TS Index Signatures object

My angular fronend sending to .net core contoler object with index signature object. Something like export interface LazyLoadEvent { first?: number; rows?: number; filters?: { [s: string]: FilterMetadata; }; } export…
adopilot
  • 4,340
  • 12
  • 65
  • 92
0
votes
1 answer

AssemblyScript compilation error: TS2329: Index signature is missing in type

I'm trying to create a sort function in AssemblyScript but am receiving this error in the function. I'd like the sort function to be re-usable and take in two possible sort parameters. I'm passing in a class that has an index signature but getting…
BVBAccelerate
  • 172
  • 1
  • 5
  • 17
0
votes
1 answer

If the condition `property in object` is true, why does TypeScript not infer the type of object[property] to be the union type of object's values?

I'm trying to access object[property], and TypeScript is throwing the element implicitly has any type error. const getValue = (key: string) => { const object = { property0: 14 property1: -3 } if (!(key in object)) { throw new…
0
votes
1 answer

Typescript index signature error : Type error: Argument of type is not assignable to parameter of type 'SetStateAction'

I'm new to typescript and to Index signatures and I don't find how to solve that error in my code. I assume that I need to something like on variable sortProperty on the variable sorted, but I can't find the answer. I have 2 errors. One on the…
Rom-888
  • 681
  • 2
  • 9
  • 30
0
votes
1 answer

Property does not exists on type when using union with index signature

Let's assume we have following interfaces: interface A { type: 'A'; value: { a: 3, }; } interface B { type: 'B'; value: { b: 3, }; } I create another interface which uses union of these interfaces: interface C { detail: A |…
milad
  • 1,854
  • 2
  • 11
  • 27
0
votes
2 answers

Typescript: How to define a type for unknown quantity of record patterns

I'm trying to use Nivo charts with typescript and I'm not sure how to define the type of data Nivo expects for their Bar Chart. (https://Nivo.Rocks) I've been trying to use Object Index Signature ... {[Key: string]: string;} Record utility type …
GatesKennedy
  • 675
  • 4
  • 11