TypeScript 在 Node.js 依赖项中找不到命名空间

TypeScript cannot find namespace in Node.js dependency

我有两个 node.js 包含类型声明文件的包。 我在包 a 中声明了一个命名空间,我想在包 b.

中引用它

套餐A

index.d.ts

declare namespace foo {
    export interface A {}
}

package.json

{
  "name": "a",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "types": "index.d.ts",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC"
}

套餐B

index.d.ts

declare namespace bar {
    const A: foo.A;
}

package.json

{
  "name": "b",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "a": "file:../a"
  }
}

错误

$ cd b
$ npm install
$ tsc --noEmit index.d.ts

index.d.ts:3:14 - error TS2503: Cannot find namespace 'foo'.

3     const A: foo.A;
               ~~~


Found 1 error.

如何获取包 b 以查看包 a 中声明的 namespace?这是代码 https://github.com/icholy/typescript_problem_example

的回购协议

我找到的最干净的解决方案是将模块作为类型导入。

declare namespace bar {
    type _ = import('a');
    const A: foo.A;
}

另一种解决方案是使用 Tripple-Slash Directive 添加对 a 的直接引用。

/// <reference types="a" />

declare namespace bar {
    const A: foo.A;
}