Reputation: 391
I'm unable to find a good resource for this question. Essentially I want to describe types for my data using an interface in TypeScript, but my data is Immutable.js records which appears to complicate matters, please see my example 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
})
])
Questions with the above:
<tree[]>
when creating the list? And then any Map added to the list be type checked against this?I've been working on this for days and I simply can't get it to work, it should be this simple according to everything I've read.
Any help would be greatly appreciated.
Upvotes: 2
Views: 1395
Reputation: 2848
You are creating a List
of arrays
of trees
, which is an extended Map
. This is what it should look like:
let trees = Immutable.List<tree[]>(
[ // List
[ // Array
<tree>Immutable.Map<any>({
id: 101,
type: 1
}),
<tree>Immutable.Map<any>({
id: 201,
type: 3
})
]
]
);
Answering your questions:
Map
that builds a map from an object. I added the <tree>
cast because it is an array of trees
, not maps.You are trying something like:
var singleTree: tree = Immutable.Map<tree>(
{ id: 101, type: 1 }
);
But your tree is a map of the any
type. So, this is the right syntax:
let singleTree: tree = <tree>Immutable.Map<any>(
{ id: 101, type: 1 }
);
For the code above, we can simplify and force a type check if we create a tree
function to wrap the Map
function, so, the final solution would be:
function tree(obj: { [key: string]: any, id: number, type: number }): tree {
return <tree>Immutable.Map<any>(obj);
}
let trees = Immutable.List<tree[]>(
[ // List
[ // Array
tree({
id: 101,
type: 1
}),
tree({
id: 201,
type: 3
})
]
]
);
Upvotes: 4