webpack / schema-utils

Options Validation
MIT License
246 stars 39 forks source link

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

Closed ozyman42 closed 4 years ago

ozyman42 commented 4 years ago

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 commented 4 years ago

/cc @arcanis

arcanis commented 4 years ago

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

alexander-akait commented 4 years ago

/cc @vankop What decision are we making?

vankop commented 4 years ago

I am fine with putting it as dependencies

sokra commented 4 years ago

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

alexander-akait commented 4 years ago

Fixed