I have this project dir tree for my npm package development:
- src/
- @types/
- index.d.ts index.ts
- @types/
- tsconfig.js
- package.json
src/index.ts contents:
import { MyObject} from "./@types";
const l:MyObject = new MyObject();
l.info("Hello");
@types/index.d.ts contents:
export type MyObject = {} | {'cool':string};
tsconfig.js contents:
{
"compileOnSave": true,
"compilerOptions": {
"lib": ["es2017"],
"removeComments": true,
"moduleResolution": "node",
"noUnusedLocals": true,
"noUnusedParameters": true,
"noImplicitReturns": true ,
"noImplicitAny": true,
"resolveJsonModule": true,
"strictNullChecks": true,
"allowSyntheticDefaultImports": true,
"sourceMap": true,
"target": "es5",
"module": "commonjs",
"declaration": true,
"outDir": "lib",
"strict": true,
"rootDir": "./src",
"types": [ "./src/@types"]
},
"include": [
"src/*.ts",
"src/@types/*.ts"
],
"exclude": [
"node_modules/**/*",
"lib"
]
}
package.json contents:
{
"name": "<my-package-name>",
"version": "1.0.1",
"description": ".... ",
"main": "lib/index.js",
"types": "lib/index.d.ts",
"scripts": {
"build": "tsc"
},
"repository": {
"type": "git",
"url": "git+https://github.com/<GITHUB_USERNAME>/<my-package-name>.git"
},
"dependencies": {
"chalk": "^4.1.2"
},
"devDependencies": {
"@types/node": "^15.14.9",
"typescript": "^4.5.2"
}
}
When I exec
npm run build
I will get the following on my lib out dir:
- lib/
- index.d.ts
- index.js
- index.js.map
Problem is that @types directory isn't exported at build under lib/ directory
So when I test and install on my other projct using the package
The project will execute, but if I investigate inside node_modules/mypackage/index.d.ts
I will see a typescript issue trying to import my type MyObject from @types folder
import { MyObject } from "./@types"; <--- Cannot find module './@types' or its corresponding type declarations.ts(2307)
What am I missing?
EDIT
updated package.json like this after comments below but still no @types in built lib/ folder:
{
...
"main": "lib/index.js",
"types": "src/@types/index.d.ts",
...
"files": [
"lib",
"src/@types"
],
...
}