Since React 16 now allows custom DOM attributes, I tried to leverage this in my Typescript code:
import * as React from 'react';
<div className="page" size="A4">
</div>
but receive this error message:
error TS2339: Property 'size' does not exist on type
'DetailedHTMLProps< HTMLAttributes< HTMLDivElement>, HTMLDivElement>'.
This thread suggests to do a module augmentation
, so I tried this way:
import * as React from 'react';
declare module 'react' {
interface HTMLProps<T> {
size?:string;
}
}
Same error message.
Finally, I also tried to declare page
as a new HTML tag:
declare global {
namespace JSX {
interface IntrinsicElements {
page: any
}
}
}
<page className="page" size="A4">
</page>
It gets rid of the error message, but the size
attribute is completely ignored in the compiled code, and I end up with:
<page className="page">
</page>
Ideally, the last one is my preferred solution. I'd like to use the size
custom attribute alongside the page
custom tag.
tsconfig.js
{
"compilerOptions": {
"outDir": "build/dist",
"module": "esnext",
"target": "es5",
"lib": ["es6", "dom"],
"sourceMap": true,
"allowJs": true,
"jsx": "react",
"moduleResolution": "node",
"rootDir": "src",
"forceConsistentCasingInFileNames": true,
"noImplicitReturns": true,
"noImplicitThis": true,
"noImplicitAny": true,
"strictNullChecks": true,
"suppressImplicitAnyIndexErrors": true,
"allowSyntheticDefaultImports": true,
"noUnusedLocals": false,
"noUnusedParameters": false,
"allowUnusedLabels": true,
"allowUnreachableCode": true
}
}
See Question&Answers more detail:
os