简体   繁体   中英

Providing typescript declaration file for external lib with Nx

This question has been asked and answered many different ways on Stack overflow and I think I've tried them all. This question is specific to getting it to work using @nrwl/nx.

The specific error is

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';`

I've tried unsuccessfully:

  1. Creating a global.d.ts file with declare module 'libxmljs2-xsd' in it.
  2. Create index.d.ts file with declare module 'libxmljs2-xsd' and tried putting it at the root of the Nx lib, inside src and inside src/lib . None of those locations worked.
  3. Modifying tsconfig.lib.json inside the root dir: "include": ["**/*.ts", "**/*.d.ts"] . I've also tried "include": ["**/*.ts", "index.d.ts"]
  4. Created types/index.d.ts at root and in the root tsconfig.base.json added "typeRoots": ["node_modules/@types", "./types"]

I realize I can set "noImplicitAny": false but don't want to do that and lose a lot of the value of typescript.

I've spent a day on this thinking there has to be something simple I'm missing. I've searched stack overflow, all of the GitHub tickets and their documentation. Does anyone know how this works in Nx?

you should add the declaration: true inside your compilerOptions object declared inside your tsconfig.json file.

Refer the official docs for more info: https://www.typescriptlang.org/tsconfig#declaration

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM