【问题标题】:Could not find the declaration file of my Typescript library even when it exists找不到我的 Typescript 库的声明文件,即使它存在
【发布时间】:2017-09-17 10:36:43
【问题描述】:

我在另一个question 中提到了完全相同的问题,但不幸的是我无法以相同的方式解决它。

基本上我写了一个 Typescript 库,现在,在发布它之前,我想在本地测试它并检查它是否被正确导入。

这是我的测试项目树:

.
├── js-src
│   └── index.js
├── node_modules
│   └── response-giver -> ../../response-giver
├── package-lock.json
├── tsconfig.json
└── ts-src
    └── index.ts

你可以直接在我的 repo 上看到我的模块:https://github.com/Giovarco/response-given/tree/develop

这就是我尝试更改模块上的tsconfig.json 以解决问题的方法:

{
    "compilerOptions": {
        "emitDecoratorMetadata" : true,
        "experimentalDecorators" : true,
        "module" : "commonjs",
        "target" : "ES5",
        "watch" : true,
        "outDir" : "js-src",
        "rootDir" : "ts-src",
        "declaration": true,
        "allowJs": false,
        "lib": [ "es2015" ],
        "typeRoots": [
            "node_modules/@types",
            "js-src/index"
        ],
        "main" : "js-src/index"

    }
}

这就是我尝试导入模块的方式:

import * as responseGiver from "response-giver";

但我收到此错误:

[ts]
Could not find a declaration file for module 'response-giver'. '/home/mario/Git projects/response-giver/js-src/index.js' implicitly has an 'any' type.
  Try `npm install @types/response-giver` if it exists or add a new declaration (.d.ts) file containing `declare module 'response-giver';`

这仍然有效:

import * as responseGiver from "../node_modules/response-giver";

但是当我尝试使用我的函数时,我没有得到正确的签名。

【问题讨论】:

    标签: typescript typescript-declarations


    【解决方案1】:

    你有 node_modules/@types/response-giver 吗?

    【讨论】:

    • 不,我不知道。但是,正如你在我的仓库中看到的那样,我在 /home/mario/Git projects/response-giver/js-src/ 下有 index.d.ts
    • 尝试在 @types/response-giver 中添加 index.d.ts 。
    • 我实际上并没有尝试您的方法,但我找到了解决方案。我会将其发布为不同的答案。还是谢谢
    【解决方案2】:

    这就是我解决问题的方法。基本上将"types": "js-src/index.d.ts" 添加到package.json

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-08-02
      • 2020-11-28
      • 1970-01-01
      • 2018-12-09
      • 1970-01-01
      • 1970-01-01
      • 2021-01-18
      • 2017-02-16
      相关资源
      最近更新 更多