找不到我的 Typescript 库的声明文件,即使它存在

Could not find the declaration file of my Typescript library even when it exists

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

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

这是我的测试项目树:

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

您可以直接在我的仓库中看到我的模块: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";

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

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

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