How Can I Use Domstringmap in Typescript?
Let's Say I Have a Function: Angular. forEach(myElements, Function prepareElements(myEl: Htmlelement, Index) { myEl. Dataset. myProperty = "Whatever"; }) the...
Let's say I have a function:
angular.forEach(myElements, function prepareElements(myEl: HTMLElement, index) {
myEl.dataset.myProperty = "whatever";
})
The problem I get is
error TS2094: The property 'myProperty' does not exist on value of type 'DOMStringMap'
I don't really understand the interface in lib.d.ts
interface DOMStringMap {
}
declare var DOMStringMap: {
prototype: DOMStringMap;
new (): DOMStringMap;
}
then later on...
interface HTMLElement {
dataset: DOMStringMap;
hidden: boolean;
msGetInputContext(): MSInputMethodContext;
}
Is it just me or is this a little unclear?
I tried casting it <DOMStringMap>myEl.dataset.myProperty = "whatever", which did nothing...
2 Answers
The DOMStringMap interface is empty because the spec is not finalized :
In the meantime you can simply use: myEl.dataset['myProperty'] = "whatever";
you can also cast it to user defined type
type IType = {myProperty: string}
....
(myEl.dataset) as IType.myProperty = "whatever";