Typescript 项目参考 常见依赖参考

问题描述

这基本上是对我的 https://github.com/RyanCavanaugh/project-references-demo/issues/10 的重新发布,以防忙碌的维护者无法发表评论

关于如何正确有效地使用 Typescript 项目引用,网络上似乎仍有很多困惑,尤其是在 monorepo 场景中。我正在尝试为自己组装这样的原型,但在构建工作时遇到了问题。

我认为@RyanCavanaugh 的例子是最好的参考。我惊讶地发现,通过简单地添加共享引用,我会遇到构建错误。也许我只是犯了一个愚蠢的错误/遗漏?

我从他的动物园示例开始,其中引用了动物园 -> 动物 -> 核心,我只是直接从动物园 -> 核心添加一个参考。但是 npm build 会产生很多错误,开头是:

animals/index.ts:1:20 - error TS6059: File 'C:/code/project-references-demo/animals/animal.ts' is not under 'rootDir' 'C:/code/project-references-demo/zoo'. 'rootDir' is expected to contain all source files.

| import Animal from './animal';

我错过了什么?

enter image description here

解决方法

修复截图:

{
  "extends": "../tsconfig-base.json","compilerOptions": {
    "outDir": "../lib/zoo","rootDir": "."
  },"references": [
    {
      "path": "../animals",},{
      "path": "../core"
    }
  ]
}