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

2 Answers

The DOMStringMap interface is empty because the spec is not finalized :

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

2

you can also cast it to user defined type

type IType = {myProperty: string}
....
(myEl.dataset) as IType.myProperty = "whatever";

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like