15

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?

Jeff
  • 939
  • 10
  • 19
  • Did you solve this situation, if so, can you provide a solution? – Nestor Sep 20 '21 at 07:14
  • 4
    I'm able to achieve this by setting `"esModuleInterop": true,` in `tsconfig.base.json` in project root folder and add `module-name.d.ts` in the project root folder. – Zesky Nov 18 '21 at 14:34
  • 1
    OMG @Zesky thanks I already tried all the possible combinations for paths – aitorllj93 Feb 27 '22 at 20:25
  • Well, I'm also stuck on this problem. I'm creating a custom theme with MUI, I created a lib in util/types/theme.d.ts e till now I could find a way to make it work across my apps (micro-frontends) – niltonxp Mar 17 '23 at 20:10

1 Answers1

-2

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

  • 1
    This is how you output .d.ts for your libraries during compilation. @Jeff is asking about the opposite problem - how to define .d.ts for already build libraries that do not have such. Therefore my downvoting. – tony.ganchev Jun 11 '23 at 11:57