简体   繁体   中英

TypeScript Declaration File not read while its folder is in typeRoots

In a typescript (Angular 9) project I'm trying to import a private JS lib.

When I import it with import { myMethod } from 'my-private-repo/dist/util'; I get the following error:

Could not find a declaration file for module 'my-private-repo/dist@types/util'. 'my-private-repo/dist/util.js' implicitly has an 'any' type.
  Try `npm install @types/my-private-repo` if it exists or add a new declaration (.d.ts) file containing `declare module 'my-private-repo/dist/util';`

I tried to fix the problem adding a declaration file in a typings folder with the following content: declare module "my-private-repo/dist/util"; , or even with declare module "*"; but the error doesn't change, like my declaration file is not read at all while I changed my ts config to include it:

{
  ...
  "compilerOptions": {
    ...
    "noImplicitAny": true,
    ...
    "typeRoots": [
      "./typings",
      "./node_modules/@types"
    ]
  }
}

I don't understand why my declaration file is not recognized.

Do you have any idea?

Thank you:)

You need to add a declaration file in one of these paths:

  1. 'my-private-repo/dist/util.d.ts'

  2. 'my-private-repo/dist/package.json' (and specify a "types" property)

  3. 'my-private-repo/dist/@types/util.d.ts'

read about how typescript load module: https://www.typescriptlang.org/docs/handbook/module-resolution.html

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