簡體   English   中英

TypeScript 聲明文件在其文件夾位於 typeRoots 時未讀取

[英]TypeScript Declaration File not read while its folder is in typeRoots

在 typescript (Angular 9) 項目中,我正在嘗試導入私有 JS 庫。

當我使用import { myMethod } from 'my-private-repo/dist/util'; 我收到以下錯誤:

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

我試圖解決在類型文件夾中添加具有以下內容的聲明文件的問題: declare module "my-private-repo/dist/util"; typings ,甚至使用declare module "*"; 但錯誤沒有改變,就像我的聲明文件根本沒有被讀取,而我更改了我的 ts 配置以包含它:

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

我不明白為什么我的聲明文件無法識別。

你有什么主意嗎?

謝謝:)

您需要在以下路徑之一中添加聲明文件:

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

  2. 'my-private-repo/dist/package.json' (並指定“類型”屬性)

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

了解如何 typescript 加載模塊: https://www.typescriptlang.org/docs/handbook/module-resolution.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM