在同级目录中的 monorepo 中引用类型(在根目录下)

Referencing types in monorepo in sibling directory (going below root)

有人知道如何帮助解决 TypeScript monorepo 项目中的引用类型问题吗?或者如果在以下设置中甚至可能。

结构像

.
├── tsconfig.json
├── lib/
│   └── workers/
│       ├── types.ts
│       ├── hello.ts
│       └── tsconfig.json
└── frontend/
    ├── tsconfig.json
    └── openwcMiniflare.ts

其中 frontend/tsconfig.json 引用类似 import { WorkerMethods, WorkerEvents } from 'workers/types'; 的类型,配置如下(相关部分,完整源代码 here

{
  "compilerOptions": {
    "composite": true,
    "baseUrl": ".",
    "outDir": "./out-tsc",
    "sourceMap": true,
    "rootDir": "./",
    "paths": {
      "workers/*": ["../lib/workers/*"]
    },
  }
  "references": [
    { "path": "../lib/workers" }
  ]
}

workers/types.ts

export type WorkerMethods = {
  sum: (x: number, y: number) => number;
  mul: (x: number, y: number) => number;
};

export type WorkerEvents = {
  ping: string;
};

我不明白 VS Code 是如何给出以下错误消息的,事实上,看起来类型在运行时未定义。

但是! 如果我像 import { WorkerMethods, WorkerEvents } from '../../lib/workers/types'; 这样更改定义,则错误消息会告诉他们在 rootDir.

之外

VS Code 甚至可以实现这样的结构吗?

嗯!这很尴尬,也有点奇怪。在我清理 node_modules 并锁定文件后,此配置确实有效。我不知道可能是什么问题,如果原因是关于这个或做这个清洁只是以这样的方式起作用,解决方案就起作用了。

无论如何,if/when 有人研究了这个问题,该配置应该有效(记住复合项目上通常的 tsc --buildtsc --project <directory>\tsconfig.json 或其组合)。