简体   繁体   English

TypeScript 声明文件在其文件夹位于 typeRoots 时未读取

[英]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.在 typescript (Angular 9) 项目中,我正在尝试导入私有 JS 库。

When I import it with import { myMethod } from 'my-private-repo/dist/util';当我使用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";我试图解决在类型文件夹中添加具有以下内容的声明文件的问题: declare module "my-private-repo/dist/util"; typings , or even with declare module "*"; ,甚至使用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:但错误没有改变,就像我的声明文件根本没有被读取,而我更改了我的 ts 配置以包含它:

{
  ...
  "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) 'my-private-repo/dist/package.json' (并指定“类型”属性)

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

read about how typescript load module: https://www.typescriptlang.org/docs/handbook/module-resolution.html了解如何 typescript 加载模块: https://www.typescriptlang.org/docs/handbook/module-resolution.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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