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

Yarn v2 + Typescript: Typings Issue @types/json-schema missing from dependencies #97

Closed
ozyman42 opened this issue May 3, 2020 · 6 comments

Comments

@ozyman42
Copy link
Contributor

ozyman42 commented May 3, 2020

  • Operating System: Ubuntu 18.04
  • Node Version: 13.14.0
  • NPM Version: Yarn v2 (Berry)
  • webpack Version: 5.0.0-beta.15
  • schema-utils Version: 2.6.6
  • typescript Version: 3.8.3

Expected Behavior

When using Yarn v2 Berry Plug-n-Play module resolution, typescript to transpile, and requiring webpack 5 as a dependency, the code should compile without issue.

yarn tsc -b

Actual Behavior

typescript gives this error when trying to compile

.yarn/cache/schema-utils-npm-2.6.6-31a26805d3-2.zip/node_modules/schema-utils/declarations/ValidationError.d.ts:94:16 - error TS2307: Cannot find module 'json-schema'.
94       | import('json-schema').JSONSchema7

Yarn v2 Plug-n-Play is strict (which is a good thing) in that it does not allow a package/workspace to require dependencies which are not listed in its package.json dependencies. Even if I add @types/json-schema to my own repo's dev dependencies, it still won't fix the issue. The only way I can solve this issue is to modify the yarn lockfile to add "@types/json-schema": ^7.0.4 to the dependencies of schema-utils.

Code

import * as webpack from 'webpack';
webpack;
console.log("hello world");

How Do We Reproduce?

I've created a demo repository

  1. Ensure you have latest stable yarn installed
  2. git clone https://github.com/AlexLeung/schema-utils-typescript-issue-demo
  3. cd schema-utils-typescript-issue-demo
  4. yarn install
  5. yarn start

Notice all these TypeScript errors related to json-schema being missing

To fix, you can:

  1. Open yarn.lock
  2. Go to line 923 to the lockfile dependencies of schema-utils and add a new line "@types/json-schema": ^7.0.4
  3. yarn install
  4. yarn start

Now you'll notice that all those json-schema errors are gone, but I'll need to open a similar issue on the webpack repo itself to solve the issue with estree.

How Do We Solve?

Move @types/json-schema from devDependencies to dependencies.

As a general rule, whenever you're writing typescript definitions such as the one for schema-utils, if you're going to include another library's types in the definitions which you export, you need to include that library's types as a dependency rather than a devDependency.

Please review the official TypeScript guidelines on Publishing. https://www.typescriptlang.org/docs/handbook/declaration-files/publishing.html See the "Dependencies" section.

@alexander-akait
Copy link
Member

/cc @arcanis

@arcanis
Copy link

arcanis commented May 4, 2020

Yep. An alternative is to define @types/json-schema as optional peer dependency, but it'll require consumers to add it themselves.

@alexander-akait
Copy link
Member

/cc @vankop What decision are we making?

@vankop
Copy link
Member

vankop commented May 25, 2020

I am fine with putting it as dependencies

@sokra
Copy link
Member

sokra commented May 25, 2020

While it's a little bit weird in my opinion, I don't see a better solution than adding it to dependencies...

alexander-akait pushed a commit that referenced this issue May 29, 2020
… (#98)

Co-authored-by: Evilebot Tnawi <evilebottnawi@users.noreply.github.com>
@alexander-akait
Copy link
Member

Fixed

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

No branches or pull requests

5 participants