使用 Nx 为外部库提供打字稿声明文件

Jef*_*eff 16 nrwl-nx

这个问题已经在堆栈溢出上以多种不同的方式提出和回答,我想我已经尝试了所有这些。这个问题特定于使用 @nrwl/nx 使其工作。

具体错误是

TS7016: Could not find a declaration file for module 'libxmljs2-xsd'. '/Users/xyz/hpxml-gen/node_modules/libxmljs2-xsd/index.js' implicitly has an 'any' type.
  Try `npm i --save-dev @types/libxmljs2-xsd` if it exists or add a new declaration (.d.ts) file containing `declare module 'libxmljs2-xsd';`
Run Code Online (Sandbox Code Playgroud)

我尝试过但没有成功:

  1. 创建一个包含在其中的 global.d.ts 文件declare module 'libxmljs2-xsd'
  2. 创建 index.d.ts 文件并declare module 'libxmljs2-xsd'尝试将其放在 Nx lib 的根目录中, insidesrc和 inside src/lib。这些地点都不起作用。
  3. 修改根目录中的 tsconfig.lib.json: "include": ["**/*.ts", "**/*.d.ts"]。我也尝试过"include": ["**/*.ts", "index.d.ts"]
  4. 在根目录下创建types/index.d.ts并在根目录下添加 tsconfig.base.json"typeRoots": ["node_modules/@types", "./types"]

我意识到我可以设置"noImplicitAny": false但不想这样做并失去打字稿的很多价值。

我花了一天的时间思考这个问题,我想一定是我错过了一些简单的东西。我搜索了堆栈溢出、所有 GitHub 票证及其文档。有谁知道这在 Nx 中是如何工作的?

小智 -2

您应该添加在 tsconfig.json 文件中声明的对象declaration: true内部。compilerOptions

有关更多信息,请参阅官方文档: https: //www.typescriptlang.org/tsconfig#declaration