Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 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...

like image 755
bodine Avatar asked Mar 21 '14 20:03

bodine


1 Answers

The DOMStringMap interface is empty because the spec is not finalized : https://developer.mozilla.org/en/docs/Web/API/DOMStringMap

In the meantime you can simply use: myEl.dataset['myProperty'] = "whatever";

like image 193
basarat Avatar answered Nov 02 '22 23:11

basarat