在typescript中在客户端和服务器之间共享代码
所以我找到了这篇文章,但没有得到答案,我现在面临着同样的问题,我的项目根目录中有一个客户端、服务器和共享文件夹,客户端和服务器的根目录中都有一个tsconfig,还有一个带有typescript文件的src文件夹,我希望能够像这样导入在typescript中在客户端和服务器之间共享代码,typescript,Typescript,所以我找到了这篇文章,但没有得到答案,我现在面临着同样的问题,我的项目根目录中有一个客户端、服务器和共享文件夹,客户端和服务器的根目录中都有一个tsconfig,还有一个带有typescript文件的src文件夹,我希望能够像这样导入 从'@shared/Promotion'导入{PromotionInterface}; 但是使用下面的tsconfig表示找不到模块 { "compilerOptions": { "target": "es5", "lib": [
从'@shared/Promotion'导入{PromotionInterface};
但是使用下面的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
或rootDir
数组中包含共享文件夹路径。以下配置适用于我:
{
"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"
]
}