typescript - 库的打字稿声明文件找不到模块
问题描述
我正在为我正在从事的几个项目开发打字稿库。它包含一些包,每个包都有一些模块,但是我遇到了 tsc 为我生成的声明文件的问题。
我创建了一个可能更容易查看的存储库。 https://github.com/BenMcLean981/typescript-library-issue
我的代码或多或少的结构如下:
.
├── src
│ ├── packageA
| | ├──moduleA.ts
│ │ └──index.ts
│ └── packageB
| ├──moduleB.ts
│ └──index.ts
└── tsconfig.json
我的 tsconfig 如下:
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"declaration": true,
"outDir": "./dist",
"strict": true,
"lib": ["ES2019"],
"sourceMap": true,
"baseUrl": "./src",
"esModuleInterop": true,
"moduleResolution": "node"
},
"include": ["src"],
"exclude": ["node_modules",]
}
现在,在 moduleA.ts 我有一些这样的代码,例如
export class Foo {
foo(): string {
return "foo";
}
}
在 packageA/index.ts 我有:
export { Foo } from "./moduleA.ts"
包B/moduleB.ts:
import { Foo} from "moduleB" //Note here that I import directly form the package.
// This is how I want my library to be consumed.
export class Bar extends Foo {
bar(): string {
return "bar";
}
}
这一切的关键是它有效。进口看起来不错,对我来说真的很容易工作。但是当我去构建它并发布它时,我在我的打字稿声明文件中得到以下内容。
模块B.d.ts
import { Foo } from "packageA"; //Cannot find module 'packageA' or its corresponding type declarations.ts(2307)
export declare class Bar extends Foo {
bar(): string;
}
我很确定这是我的 tsconfig 的问题。我不明白所有的设置。任何帮助将非常感激!
解决方案
正如Amir Saleem建议的那样,将以下内容添加到我的 tsconfig 解决了我的问题:
"declarationMap": true
推荐阅读
- vue.js - 如何创建一个可以由 Nuxt 导入的动态导入库?
- javascript - 一个接一个地运行几个 exec() 命令
- .net - 优化父母和孩子的 EF 插入
- c++ - 我的 C++ 合并排序代码不起作用。我在这里想念什么?
- elasticsearch - 在启用 include_in_parent 的情况下,针对 elasticsearch 中嵌套字段的嵌套查询与非嵌套查询
- centos - Webmin 错误 - 通过文件管理器更改目录的权限
- php - PHP regex - 将纯文本除以字符串对象
- sql - 使用 Apache Spark SQL 对结果进行子分组
- sql - 根据特定条件过滤 postgres 值
- flutter - 为什么它不改变按钮的高度和宽度