Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

It references "react" in the public typing declarations but doesn't add a dependency for "@types/react" #1359

Closed
slavafomin opened this issue Oct 11, 2019 · 11 comments · Fixed by #1389 · May be fixed by novalina26/anchor#1
Closed

Comments

@slavafomin
Copy link

slavafomin commented Oct 11, 2019

Hello!

Thank you for this great plugin!

However, in published index.d.ts file you are referencing the react package in some exports of yours. However, you don't specify the dependency for @types/react, which causes issues in projects where TypeScript compiler is running in "strict" mode and where dependencies are installed by a more strict package manager like pnpm (Rush).

It gives the following TypeScript error during application compilation:

ERROR in ~/app/common/temp/node_modules/.registry.npmjs.org/react-hot-loader/4.12.15_react-dom@16.10.2+react@16.10.2/node_modules/react-hot-loader/index.d.ts(1,24):

TS7016: Could not find a declaration file for module 'react'.
'~/app/common/temp/node_modules/.registry.npmjs.org/react/16.10.2/node_modules/react/index.js' implicitly has an 'any' type.
If the 'react' package actually exposes this module, consider sending a pull request to amend 'https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/react`

As you have react in your peer dependencies, you should also add @types/react there as well in order for TypeScript projects to be able to resolve typing declarations correctly.

Thanks!

@theKashey
Copy link
Collaborator

That's bad. That means that I have to change peer dependencies for all 100500 packages I've ever created.

Is there any pnpm issue to read something about required changes (and ways to test them), as well as "script" TS mode?

@theajr
Copy link
Contributor

theajr commented Oct 12, 2019

@slavafomin Can you please provide the steps to reproduce?

@slavafomin
Copy link
Author

@theajr I can't give you a detailed example, but the gist of it is that you need to create a TypeScript React application using pnpm instead of npm (it handles dependencies in a more strict and correct manner), then replace react-dom with implementation from this package using Webpack aliases. Also, the TypeScript should be set to a "strict" mode. I'm using ForkTsCheckerWebpackPlugin to perform typing checks and Babel to transpile from TypeScript.

@slavafomin
Copy link
Author

@theKashey

That means that I have to change peer dependencies for all 100500 packages I've ever created.

You only need to specify @types/... dependencies as a non-dev dependencies if you are exposing them in your TypeScript declarations. This is a rare case, actually, and it's very easy to miss.

@slavafomin
Copy link
Author

slavafomin commented Oct 14, 2019

For those, who are using Rush the following workaround could be used to mitigate the situation until it's fixed in this package:

// common/config/rush/pnpmfile.js

'use strict';

const rewriteRules = {
  // @todo: remove this rule when the issue is fixed
  // @url https://github.com/gaearon/react-hot-loader/issues/1359
  'react-hot-loader': {
    setDependencies: {
      '@types/react': '>=16',
    },
  },
};

// https://rushjs.io/pages/configs/pnpmfile_js/

module.exports = {
  hooks: {
    readPackage: rewriteManifest,
  },
};


function rewriteManifest(manifest, context) {

  normalizeManifest(manifest);

  const { name, dependencies } = manifest;

  const rule = rewriteRules[name];
  if (rule) {
    Object.assign(dependencies, rule.setDependencies);
    context.log(`Rewritten package "${name}" dependencies`);
  }

  return manifest;

}

function normalizeManifest(manifest) {
  if (!manifest.dependencies) {
    manifest.dependencies = {};
  }
}

Feel free to add other dependencies in the rewriteRules config if you need it ;)

@theKashey
Copy link
Collaborator

Fixed in 4.12.16

@theKashey
Copy link
Collaborator

@slavafomin - would it work if @types/react would be a peerDependency?

@theKashey theKashey reopened this Nov 8, 2019
@slavafomin
Copy link
Author

@theKashey yes, I would recommend to add it as a peer dependency.

@SleeplessByte
Copy link

It should be added both as a peerDependency and devDependency and not as a dependency (re: #1391)

@theKashey
Copy link
Collaborator

4.12.17 released with a fix

@SleeplessByte
Copy link

Works!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants