Those who switched to TypeScript may experience problems with non-standard JSX/HTML props/attributes. React's type definition file () only includes standard HTML and React elements and props/attributes.
Consider, for example, that you need to allow the prop of type on the element. In TypeScript+JSX, the element uses the interface declared in as follows:
To allow the custom HTML attribute, add the member to the interface:
This approach makes use of interface merging, a form of TypeScript's declaration merging.
@types/react/index.d.ts
banana
string
<img>
<img>
React.ImgHTMLAttributes<T>
@types/react/index.d.ts
banana?: string;
React.ImgHTMLAttributes<T>
declare namespace React {
//...
interface ImgHTMLAttributes<T> extends HTMLAttributes<T> {
alt?: string;
crossOrigin?: "anonymous" | "use-credentials" | "";
decoding?: "async" | "auto" | "sync";
height?: number | string;
// ...
}
//...
}
// Inside your types.d.ts
namespace React {
interface ImgHTMLAttributes<T> {
banana?: string;
}
}