Photo by Ningyu He on Unsplash
Installing different packages and frameworks means you run into some interesting problems with TypeScript. One recently was using custom HTML Attributes on DOM elements.
declare module 'react' {
interface HTMLAttributes<T> extends AriaAttributes, DOMAttributes<T> {
// extends React's HTMLAttributes
custom?: string;
}
}
This piece of code solved my issue, and allows you to add any custom HTML attributes when using TypeScript.
<div custom="no_ts_errors">
your content here
</div>
Top comments (2)
Do you know if it's possible to limit this to specific DOM elements? This allows all DOM elements to take 'custom' as an attribute, but what if we just wanted to limit it to 1 specific DOM element?
create any file with extension .d.ts in your project
and just extend the button interface in the JSX namespace