【问题标题】:Typescript declaration file for library cannot find module库的打字稿声明文件找不到模块
【发布时间】:2021-09-20 05:48:30
【问题描述】:

我正在为我正在从事的几个项目开发打字稿库。它包含一些包,每个包都有一些模块,但我遇到了 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"

packageB/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";
  }
}

这一切的关键在于它有效。进口看起来不错,对我来说真的很容易工作。但是当我去构建它并发布它时,我会在我的打字稿声明文件中得到以下内容。

moduleB.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 的问题。我不明白所有的设置。任何帮助将不胜感激!

【问题讨论】:

  • 尝试将"declarationMap": true, 添加到您的 tsconfig 文件中

标签: typescript tsc


【解决方案1】:

正如Amir Saleem 建议的那样,将以下内容添加到我的 tsconfig 解决了我的问题:

"declarationMap": true

【讨论】:

    猜你喜欢
    • 2018-11-06
    • 2019-01-07
    • 1970-01-01
    • 1970-01-01
    • 2020-11-27
    • 1970-01-01
    • 1970-01-01
    • 2017-11-01
    • 2022-01-06
    相关资源
    最近更新 更多