0

我有这个项目目录树用于我的 npm 包开发:

  • 源/
    • @types/
      • 索引.d.ts 索引.ts
  • tsconfig.js
  • 包.json

src/index.ts 内容:

import { MyObject} from "./@types";
const l:MyObject = new MyObject();
l.info("Hello");

@types/index.d.ts 内容:

export type MyObject = {} | {'cool':string};

tsconfig.js 内容:

{
  "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 内容:

{
  "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"
  }
}

当我执行

npm run build

我将在我的 lib out 目录中获得以下信息:

  • 库/
    • 索引.d.ts
    • index.js
    • index.js.map

问题是 @types 目录没有在 lib/ 目录下构建时导出

因此,当我使用该软件包在其他项目上进行测试和安装时

该项目将执行,但如果我在 node_modules/mypackage/index.d.ts 内部进行调查

我会看到一个打字稿问题,试图从 @types 文件夹导入我的类型 MyObject

import { MyObject } from "./@types"; <--- Cannot find module './@types' or its corresponding type declarations.ts(2307)

我错过了什么?

编辑

在下面的评论之后像这样更新了 package.json 但在构建的 lib/ 文件夹中仍然没有 @types:

{
  ...
  "main": "lib/index.js",
  "types": "src/@types/index.d.ts",
  ...
  "files": [
    "lib",
    "src/@types"
  ],
  ...
}
4

0 回答 0