I have a JavaScript object with the following structure:
{
"_index": 1,
"foo": { ... },
"bar": { ... },
"baz": { ... },
}
That is, the object will always have one property _index with a number value, and then all the other keys (which aren't defined since its being used as a map) have a value of another, specific, object.
I have tried the following types in TypeScript
type Properties = {
_index: number
}
type Fields = {
// its not important what Metadata is, just that it has a type that isn't number
[field: string]: Metadata
}
type Lookup = Properties & Fields
This works well for accessing an already created instance of Lookup in TypeScript, but I don't seem to be able to create an instance of Lookup in TypeScript.
For example, if I do this:
let lookup: Lookup = {
_index: 1,
foo: { ... }
}
I get the following error:
2322[QF available]: Type '{ _index: number; "foo": { ... }' is not assignable to type 'Lookup'. Type '{ _index: number; "foo": { ... }' is not assignable to type 'Fields'. Property '_index' is incompatible with index signature. Type 'number' is not assignable to type 'Metadata'
What is the best way to express this type in TypeScript?