Anyone know how to properly add/extend all native HTML element attributes with custom ones?
With the TypeScript documentation for merging interfaces, I thought that I could just do this:
interface HTMLElement {
block?: BEM.Block;
element?: BEM.Element;
modifiers?: BEM.Modifiers;
}
<div block="foo" />; // error
But I get the following Intellisense error in vscode 1.6.1 (latest):
[ts] Property 'block' does not exist on type 'HTMLProps'.
The HTMLProps
to which they are referring is React.HTMLProps<T>
and the div
element is declared to use it like so:
namespace JSX {
interface IntrinsicElements {
div: React.HTMLProps<HTMLDivElement>
}
}
I tried redeclaring the div
, but to no avail.
Related: https://github.com/Microsoft/TypeScript/issues/11684
Edit: Here's what ended up working for me:
declare module 'react' {
interface HTMLAttributes<T> extends DOMAttributes<T> {
block?: string
element?: string
modifiers?: Modifiers // <-- custom interface
}
}