[英]Lerna with Yarn, TypeScript and React Native: Cannot find module '@project/common' or its corresponding type declarations.ts(2307)
[英]Lerna, yarn, and Typescript: Cannot find module or its corresponding type declarations
我有一个 lerna + yarn 工作区 monorepo,具有独立的版本控制。 我在其兄弟姐妹中导入 package 时遇到问题。
我有两个包,其中一个应该依赖于另一个,如下图所示:
(root)
|--packages
|--money
|--money-standard-units
|--{deps. on money}
在money-standard-deps
中,我尝试导入money
的导出成员,但我无法这样做; 我收到以下消息:
TS2307:找不到模块“金钱”或其相应的类型声明。
我知道这个问题可以通过将money
添加到money-standard-libs
的 tsconfig 的paths
数组来解决,但我想依赖于内置的 package 因为这些将单独发布。 我希望它能够像两个单独的存储库一样有效地工作。
我创建了一个演示仓库。
如果要将其用作另一个 typescript 项目的依赖项,则需要配置 typescript 项目以生成类型声明*.d.ts
文件。 为此,您需要将 tsconfig 的compilerOptions.declaration
属性设置为true
。 您可以在tsconfig-common.json文件中执行此操作。
例如(tsconfig-common.json):
{
...
"declaration": true
...
}
此外,以同样的方式在package.json
文件中指定main
属性,以识别 package 的入口文件,您还需要指定types
声明文件所在的位置。 您需要为 monorepo 中的每个 package 执行此操作。
例如(package.json):
{
"name": "@somescope/money",
...
"main": "dist/index.js",
"types": "dist/index.d.ts",
...
}
另外,我注意到您在货币单位模块中进行了默认导出,但随后在使用货币模块中对其进行了命名导入。
如果您打算使用命名导入来引用它,则需要更改monetary-unit
模块以在 class 上使用命名导出。
示例(货币单位.ts):
// Remove the default keyword
export class MonetaryUnit {
constructor(
readonly value: number,
readonly name: string,
) {}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.