You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When imported in a TypeScript environment, eslint-plugin-react-hooks throws a "missing type declarations" error.
Why
This is because eslint-plugin-react-hooks does not have any type declarations bundled in the package. These would usually be in an index.d.ts file.
Also, eslint-plugin-react-hooks does not have a corresponding @types/... package in the DefinitelyTyped project for users to rely on.
Workaround
Users can follow the instructions in the error message and make a temporary module augmentation to declare their own types for the package.
In my personal project, I've done it this way:
// ./src/@types/eslint-plugin-react-hooks.d.tsdeclare module 'eslint-plugin-react-hooks'{importtype{ESLint}from'eslint';constplugin: Omit<ESLint.Plugin,'configs'>&{// eslint-plugin-react-hooks does not use FlatConfig yetconfigs: Record<string,ESLint.ConfigData>;};exportdefaultplugin;}
But this will not automatically stay up-to-date with the package and is not guaranteed to be correct. Is it also incomplete and does not contain detailed information about the different configs in the plugin.
How to fix
There are several ways.
Usually the preferred solution is just to write everything in TypeScript and let it auto-generate declaration files for you. But I understand if a complete rewrite is not on the table :)
You could add a @types/eslint-plugin-react-hooks package in the DefinitelyTyped repo. But then your types would be defined in a completely different place from your actual code, and if you ever decide to migrate to TypeScript in the future, the old @types/... package would need to be deprecated. Sounds messy.
Best solution, in my opinion. Just add a index.d.ts file to the package that declare the type exports of the package. Then define it as the type declarations in package.json. Easy peasy.
After trying to add a index.d.ts declaration file myself, I quickly realized I do not understand the build/bundling system for this monorepo. The declaration files were excluded from the build output.
I found the two different TODO comments explaining how the current build process isn't great for an eslint plugin.
// TODO: it's awkward to create a bundle for this but if we don't, the package// won't get copied. We also can't create just DEV bundle because it contains a// NODE_ENV check inside. We should probably tweak our build process to allow// "raw" packages that don't get bundled.
Fixing this would also allow adding declaration files to the published package, instead of being ignored by the bundler.
Makes me wonder if eslint-plugin-react-hooks should be in its own repository like most standalone npm packages, to avoid the awkwardness of a monorepo bundling system that wasn't designed for it. But I'll leave that decision to the maintainers.
all
low
What
When imported in a TypeScript environment,
eslint-plugin-react-hooks
throws a "missing type declarations" error.Why
This is because
eslint-plugin-react-hooks
does not have any type declarations bundled in the package. These would usually be in anindex.d.ts
file.Also,
eslint-plugin-react-hooks
does not have a corresponding@types/...
package in the DefinitelyTyped project for users to rely on.Workaround
Users can follow the instructions in the error message and make a temporary module augmentation to declare their own types for the package.
In my personal project, I've done it this way:
But this will not automatically stay up-to-date with the package and is not guaranteed to be correct. Is it also incomplete and does not contain detailed information about the different configs in the plugin.
How to fix
There are several ways.
Usually the preferred solution is just to write everything in TypeScript and let it auto-generate declaration files for you. But I understand if a complete rewrite is not on the table :)
You could add a
@types/eslint-plugin-react-hooks
package in the DefinitelyTyped repo. But then your types would be defined in a completely different place from your actual code, and if you ever decide to migrate to TypeScript in the future, the old@types/...
package would need to be deprecated. Sounds messy.Best solution, in my opinion. Just add a
index.d.ts
file to the package that declare the type exports of the package. Then define it as the type declarations inpackage.json
. Easy peasy.I would be happy to do this myself. I just wanted to submit an issue first to confirm that the maintainers would approve.
Steps To Reproduce
typescript
,eslint-plugin-react-hooks
.ts
fileeslint-plugin-react-hooks
Code example
package.json
index.ts
(You'll need a
tsconfig.json
file too, but the settings are irrelevant to this example)The current behavior
TypeScript build error.
The expected behavior
No errors.
The text was updated successfully, but these errors were encountered: