I have two local packages, package1 and package2, and I want to use a TypeScript type Car declared in a separate file in and exported from package1 in package2.
The structure of package1 is:
package1/
src/
index.ts
car.d.ts
lib/
index.d.ts // generated
tsconfig.json
package.json
index.ts from package1 has only a single line:
export {Car} from './car';
So it's just re-exporting the type declared in car.d.ts, which may be something like
export interface Car {
brand: string;
};
Now I want to use local package package1 in local package package2. After running
npm i local/path/to/package1
I get the following structure for package2:
package2/
src/
index.ts
lib/
node_modules/
package1/ // link to other package
tsconfig.json
packjage.json
Everything ok so far. index.ts from package2 now tries to import Car:
import {Car} from './package1';
But that does not work; more precisely, Car is suddenly interpreted as a synonym to any (so typing does not work)! Even worse, I cannot compile package2 because applying tsc to it gives me: "error TS2307: Cannot find module './car' or its corresponding type declarations."
My tsconfig.json for both packages is the following:
{
"compilerOptions": {
"target": "es2016",
"module": "commonjs",
"declaration": true,
"strict": true,
"removeComments": false,
"sourceMap": true,
"declarationMap": true,
"downlevelIteration": true,
"esModuleInterop": true,
"rootDir": "./src",
"outDir": "./lib"
},
"include": ["src/**/*"],
"exclude": ["node_modules", "**/*.spec.ts"]
}