【问题标题】:Parcel does not generate type declarations for a TypeScript libraryParcel 不会为 TypeScript 库生成类型声明
【发布时间】:2021-12-31 21:28:15
【问题描述】:

我刚开始使用 Parcel,我遇到了这个问题,它没有生成类型声明,而是发出一个空的 index.d.ts 文件。这是我在package.json中的配置:

{
  // ...
  "scripts": {
    "prepare": "yarn build",
    "check": "tsc --noEmit",
    "watch": "parcel watch --no-source-maps --no-cache",
    "build": "parcel build --no-source-maps --no-cache"
  },
  "main": "./lib/index.js",
  "source": "./src/index.ts",
  "types": "./lib/index.d.ts",
  "module": "./lib/esm/index.js",
  "devDependencies": {
    "@parcel/packager-ts": "^2.0.1",
    "@parcel/transformer-typescript-types": "^2.0.1",
    "@types/node": "^16.11.9",
    "parcel": "^2.0.1",
    "typescript": "^4.5.2"
  },
  // ...
}

下面是我在代码中定义的type 的简单示例:

export type Response = {
  /**
   * The list of added/removed hashes
   */
  hashes: string[];
  /**
   * The list of skipped hashes
   */
  skipped: string[];
};

任何想法为什么会发生这种情况?我是否配置错误?我已关注this 文档。

谢谢。

【问题讨论】:

  • 您的配置看起来不错,但是当我只包含示例 Response 类型时,我无法重现该问题。你有没有机会包含一个更完整的回购?我现在正在修复这里的一些错误——如果你的源代码中有模块扩充,你可能会点击this one
  • 好的,感谢您的帮助!你可以找到存储库here(你只需要返回一些提交)但是我已经将项目从 Parcel 切换到了裸 TypeScript 编译器,这完成了我想要的。

标签: javascript typescript typescript-typings parceljs


【解决方案1】:

您的问题似乎是由tsconfig.json"compilerOptions" 中的"incremental": true 行引起的(请参阅here)。如果删除此行,parcel 应按预期生成 .d.ts 文件。

(在理想情况下,parcel 应该能够处理这种情况 - 我用 parcel 提交了this bug 以报告问题 - 并this PR 解决它)。

【讨论】:

  • 感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-14
  • 1970-01-01
  • 2013-08-20
相关资源
最近更新 更多