I'm struggling to find a suitable solution for my query. I am aiming to define data types using an interface in TypeScript, but my data consists of Immutable.js records making it more complex. Please refer to the example provided below.
interface tree extends Immutable.Map<string, any> {
readonly id: number,
readonly type: number
}
let trees = Immutable.List<tree[]>([
Map<tree>({
id: 101,
type: 1
}),
Map<tree>({
id: 201,
type: 3
})
])
The following questions arise:
- Why is it necessary to repeat the type of each map in my list? Shouldn't the type be declared simply by
<tree[]>
when creating the list? And then any Map added to the list should be type checked against this? - In the current scenario, this example throws an error stating that "property 'id' is incompatible with index signature. Type 'number' is not assignable to type 'tree'. This makes sense! Nevertheless, despite going through the documentation, I am unable to figure out how to resolve this issue. The docs mention the requirement for an ID, but I want an array of maps - my signature here just includes standard array IDs, if I am not mistaken?
I have been grappling with this problem for days and am at a loss. It seems like this should be straightforward based on everything I've read.
Your assistance would be greatly appreciated.