将 Angular 添加到 Monorepo 时 Typescript 构建失败(Jasmine 和 Jest 类型冲突)
Typescript Build Fails When Adding Angular into Monorepo (Clashing of Jasmine and Jest Types)
要点
我有一个只有 Typescript 包的 Monorepo。当我 运行 tsc --build
所有包将被编译为 Javascript。在我添加一个包含 Angular 应用程序的包之前,这一切正常。
为了测试我的包,我使用 Jest。但是 Angular 使用 Jasmine。
问题
现在运行宁tsc --build
的时候,好像Jasmine和Jest的类型冲突了
我总共得到 184 个错误。来自 node_modules/@types/jest
和 node_modules/@types/jasmine
以及我自己的包:
错误
我只显示了错误日志中的一些片段,因为粘贴整个内容太长了。您可以在此处找到完整的错误日志:https://gist.github.com/flolude/115e92ca13cd8c86a4dca03528b92d4f
开玩笑
node_modules/@types/jest/index.d.ts:1310:9 - 错误 TS2717:后续 属性 声明必须具有相同类型。 属性 'message' 必须是 'string' 类型,但这里有 'string | (() => string)'.
类型
1310 message: string | (() => string);
~~~~~~~
茉莉
node_modules/@types/jasmine/ts3.1/index.d.ts:304:9
304 message?: string;
~~~~~~~
'message' was also declared here.
我自己的包
services/gateway/src/gateway.server.ts:7:24 - error TS2307: Cannot find module '@cents-ideas/utils'.
7 import { Logger } from '@cents-ideas/utils';
~~~~~~~~~~~~~~~~~~~~
解决方案
当我从 Angular 项目的 package.json 中删除 Jasmine 类型时,一切都编译无误。这就是为什么我确定他们导致了错误。但是我需要那些包来用 Angular.
进行测试
"devDependencies": {
// no errors when I remove those 2 packages
"@types/jasmine": "~3.3.8",
"@types/jasminewd2": "~2.0.3",
// ...
}
源代码
正如我已经提到的,我在 Monorepo 中运行。如果您需要进一步了解,这里是源代码:
- Project root
- Angular project(导致错误)
我也运行遇到过这个问题,我可以想到两种可能的方法:
- 不要在您的 Angular 项目中使用 Jasmine。
- 使用类型根
不要在您的 Angular 项目中使用 Jasmine。
由于您似乎要开始一个新的 Angular 项目并且实际上没有编写任何基于 Jasmine 的 Angular 测试,所以您也可以为该项目使用 Jest。如果 Angular 项目中已经有 Jasmine 测试,迁移也很容易。
使用 TypeRoots
使用tsconfig typeRoot compilerOptions,您可以专门提取并修复相应的冲突。d.ts 声明。
what you need is to "break the tie" between the two declarations in some way. so options, consider creating a new folder: "overrides" , and add "overrides\jest\index.d.ts" to be what you thinkjestshould be. then update your tsconfig.json to include"typeRoots": [".\overrides", ".\node_modules@types"]. What this does it tells the compiler to look for types first in".\overrides\jest"and if it did not find it to look in".\node_modules@types\jest"`. GitHub Source
但是,您现在必须维护基本上是类型定义文件的本地分支。如果可以,您应该 post 在您认为应该修复的任何库中修复上游。
5 月 22 日更新:删除了 --skipLibCheck,因为它与 OP 问题不兼容
要点
我有一个只有 Typescript 包的 Monorepo。当我 运行 tsc --build
所有包将被编译为 Javascript。在我添加一个包含 Angular 应用程序的包之前,这一切正常。
为了测试我的包,我使用 Jest。但是 Angular 使用 Jasmine。
问题
现在运行宁tsc --build
的时候,好像Jasmine和Jest的类型冲突了
我总共得到 184 个错误。来自 node_modules/@types/jest
和 node_modules/@types/jasmine
以及我自己的包:
错误
我只显示了错误日志中的一些片段,因为粘贴整个内容太长了。您可以在此处找到完整的错误日志:https://gist.github.com/flolude/115e92ca13cd8c86a4dca03528b92d4f
开玩笑 node_modules/@types/jest/index.d.ts:1310:9 - 错误 TS2717:后续 属性 声明必须具有相同类型。 属性 'message' 必须是 'string' 类型,但这里有 'string | (() => string)'.
类型1310 message: string | (() => string);
~~~~~~~
茉莉
node_modules/@types/jasmine/ts3.1/index.d.ts:304:9
304 message?: string;
~~~~~~~
'message' was also declared here.
我自己的包
services/gateway/src/gateway.server.ts:7:24 - error TS2307: Cannot find module '@cents-ideas/utils'.
7 import { Logger } from '@cents-ideas/utils';
~~~~~~~~~~~~~~~~~~~~
解决方案
当我从 Angular 项目的 package.json 中删除 Jasmine 类型时,一切都编译无误。这就是为什么我确定他们导致了错误。但是我需要那些包来用 Angular.
进行测试"devDependencies": {
// no errors when I remove those 2 packages
"@types/jasmine": "~3.3.8",
"@types/jasminewd2": "~2.0.3",
// ...
}
源代码
正如我已经提到的,我在 Monorepo 中运行。如果您需要进一步了解,这里是源代码:
- Project root
- Angular project(导致错误)
我也运行遇到过这个问题,我可以想到两种可能的方法:
- 不要在您的 Angular 项目中使用 Jasmine。
- 使用类型根
不要在您的 Angular 项目中使用 Jasmine。
由于您似乎要开始一个新的 Angular 项目并且实际上没有编写任何基于 Jasmine 的 Angular 测试,所以您也可以为该项目使用 Jest。如果 Angular 项目中已经有 Jasmine 测试,迁移也很容易。
使用 TypeRoots
使用tsconfig typeRoot compilerOptions,您可以专门提取并修复相应的冲突。d.ts 声明。
what you need is to "break the tie" between the two declarations in some way. so options, consider creating a new folder: "overrides" , and add "overrides\jest\index.d.ts" to be what you thinkjestshould be. then update your tsconfig.json to include"typeRoots": [".\overrides", ".\node_modules@types"]. What this does it tells the compiler to look for types first in".\overrides\jest"and if it did not find it to look in".\node_modules@types\jest"`. GitHub Source
但是,您现在必须维护基本上是类型定义文件的本地分支。如果可以,您应该 post 在您认为应该修复的任何库中修复上游。
5 月 22 日更新:删除了 --skipLibCheck,因为它与 OP 问题不兼容