0

I have this project dir tree for my npm package development:

  • src/
    • @types/
      • index.d.ts index.ts
  • 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"
  ],
  ...
}
koalaok
  • 5,075
  • 11
  • 47
  • 91
  • Do you have `@types` folder anywhere in the package i.e under the path `node_modules\mypackage`? – Nalin Ranjan Jan 14 '22 at 09:21
  • You can check [files](https://docs.npmjs.com/cli/v8/configuring-npm/package-json#files) property under package.json to include `@types` folder in the package to be distributed. – Nalin Ranjan Jan 14 '22 at 09:23
  • @NalinRanjan I tried your suggestion but still no folder `@types` in lib outDir... pls. see the edit above – koalaok Jan 14 '22 at 09:51
  • You are keeping @types under `src`. Can you keep it anywhere else ? Are you writing the type definitions under `@types` yourself? – Nalin Ranjan Jan 14 '22 at 11:04
  • If you are writing the type definition(s) yourself and keeping them inside @types folder, is it possible for you to rename that folder to something else ? – Nalin Ranjan Jan 14 '22 at 11:13
  • Yes `@types` is a folder created by my with my types inside. Renamed it `typings` but still will not be created under lib. – koalaok Jan 14 '22 at 11:22
  • 1
    `tsc` will only transpile `.ts` files to their corresponding `.js` representation (with optional `.d.ts` declaration files generated alongside). If you want a more complex build system you likely want to use something more on top of just `tsc`. I usually use [rollup](https://rollupjs.org/) – Olian04 Jan 14 '22 at 11:25
  • @Olian04 thanks. your comment guided me to the aswer I was lookig for address here: https://stackoverflow.com/questions/56018167/typescript-does-not-copy-d-ts-files-to-build – koalaok Jan 14 '22 at 12:08
  • Closing because OP discovered it was a duplicate question. [Typescript does not copy d.ts files to build](https://stackoverflow.com/questions/56018167/typescript-does-not-copy-d-ts-files-to-build) – Olian04 Jan 14 '22 at 12:09

0 Answers0