This is a TypeScript project bootstrapped with create-react-app
, here are my imports:
Here is my tsconfig.json
{
"compilerOptions": {
"target": "es5",
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"noFallthroughCasesInSwitch": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react-jsx"
},
"include": [
"src"
],
"files": [
"src/typings/index.d.ts"
]
}
The issue appears to be the declaration of the oninputreport
function on the HIDDevice
class as such:
/*~ https://wicg.github.io/webhid/#hiddevice-interface */
declare class HIDDevice extends EventTarget {
oninputreport: ((this: this, ev: HIDInputReportEvent) => any) | null;
// etc
}
Using this
here seems to constrain the function to force it to be implemented either on HIDDevice
or else if implemented on another class it forces it to use this
as the parameter name, which causes other problems...here's an example of what I mean:
Note above the TS compiler is complaining that my implementation of that function is not assignable b/c the parameter name is not matching. I attempted to underscore it, but it still does not like it:
So, I tried to override it using a typings
file, but that still isn't working. The only hack workaround at this point is to cast it to unknown
and then cast it back which is realistically a very poor hack.
Of course, if I name the parameter this
in my function implementation, well this causes me other issues:
I can no longer use this
as intended on my own class.
The fix here seems fairly simple. In the spec / implementation of the typings, you could re-do the declaration like so:
/*~ https://wicg.github.io/webhid/#hiddevice-interface */
declare class HIDDevice extends EventTarget {
oninputreport: ((dev: HIDDevice, ev: HIDInputReportEvent) => any) | null;
// etc
}
I see several other areas of the typings spec where this
is used quite a bit. I would advise against this if possible.