How to have TSLint resolve indirect typing dependencies with Yarn workspaces? How to have TSLint resolve indirect typing dependencies with Yarn workspaces? typescript typescript

How to have TSLint resolve indirect typing dependencies with Yarn workspaces?


You can make TSLint work in your case by removing these lines from tsconfig.json:

"baseUrl": "./packages","paths": {  "*": ["./*/src"]},

These lines tell TypeScript compiler and TSLint that they should not treat your modules a and b as packages when you import them, but rather they should resolve individual TypeScript files using baseUrl and paths parameters and then compile individual TypeScript files. This behavior is documented in Module Resolution -> Path Mapping section of TypeScript documentation:

https://www.typescriptlang.org/docs/handbook/module-resolution.html#path-mapping

Instead, if I understood you right, you want to treat a and b as independent packages. To achieve this you should remove path mapping, and then TypeScript and TSLint will treat them as npm packages.

UPDATE (based on discussion in comments)

In your project you run TSLint using command:

tslint -p tsconfig.jsonbut you run TSC using command:

tsc src/index.ts --outDir dist

Your TSLint uses TypeScript compiler API to do checks, based on rules from tsconfig.json. But your TypeScript compiler do not use tsconfig.json rules. In real-world projects both commands will use tsconfig.json

When you start using tsconfig.json for compilation too you will get the same problem with resolving 2nd degree dependencies types as you have with TSLint:

$ tsc -p tsconfig.json../b/src/index.ts:1:23 - error TS7016: Could not find a declaration file for module 'camelcase'. '/home/victor/work/tslint-yarn-workspaces.org/node_modules/camelcase/index.js' implicitly has an 'any' type.  Try `npm install @types/camelcase` if it exists or add a new declaration (.d.ts) file containing `declare module 'camelcase';`1 import camelCase from 'camelcase'                    ~~~~~~~~~~~

This happens because path-mapped module imports compiled differently by design, then normal imports from node_modules according to TypeScript documentationhttps://www.typescriptlang.org/docs/handbook/module-resolution.html#path-mappingas specified in the first part of the answer.

I would recommend using normal imports in your project to not have troubles with tools:

  1. Have "watch": "lerna run --parallel -- watch" script in workspace root package.json
  2. Have "watch": "tsc -p tsconfig.json -w" in workspace packages.
  3. Whenever you make changes to your project - start TypeScript compiler in watch mode in every package by running npm run watch in the workspace root.