Is there a way to add a custom type to a react HTML element? I'm using React 16.13.1 with Create React App and Typescript 3.9.5. The only solution I have found is to either disable type checking, which is not ideal, or declare the react module interface at the top of every single component (as below). Is there a way to add this globally? Or a different solution?
declare module 'react' {
interface HTMLAttributes<T> {
customAttribute?: any;
// more attributes...
}
}
I have tried adding it to a created index.d.ts file and adding to tsconfig.json "compilerOptions": {"types":["./index.d.ts"]} but I still get
TS2322: Type '{ children: Element[]; customAttribute: string; className: string; }' is not assignable to type 'DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>'. Property 'customAttribute' does not exist on type
Thanks for any help.
index.d.tsin your project and the types should be available globallyindex.d.tsfile but that I was still getting the TS2322 error.