所以我发现这篇文章在separat TypeScript项目之间共享接口,但没有得到答案,我现在面临相同的问题,我在项目根目录中有一个客户端,服务器和共享文件夹,客户端和服务器都有tsconfig在他们的根目录和带有打字稿文件的src文件夹中,我希望能够像
import { PromotionInterface } from '@shared/Promotion';
但是使用下面的tsconfig表示找不到模块
{
"compilerOptions": {
"target": "es5",
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"module": "esnext",
"moduleResolution": "node",
"rootDirs": [
"src",
"../shared"
],
"paths": {
"@shared/*": [
"../shared/*"
]
},
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "preserve"
},
"include": [
"src"
]
}
有没有办法使这项工作?
您需要baseUrl
按照以下tsconfig错误中的指示将存储库的根目录指定为:
Option 'paths' cannot be used without specifying '--baseUrl' option.ts
然后,您可以指定相对于基本URL的路径。
"baseUrl": "../",
"paths": {
"@shared/*": [
"shared/*"
]
},
我认为您不需要在include
orrootDir
数组中包含共享文件夹路径。以下配置对我有用:
{
"compilerOptions": {
"target": "es5",
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"module": "esnext",
"moduleResolution": "node",
"rootDirs": [
"src"
],
"baseUrl": "../",
"paths": {
"@shared/*": [
"shared/*"
]
},
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "preserve"
},
"include": [
"src"
]
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句