Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在typescript中在客户端和服务器之间共享代码_Typescript - Fatal编程技术网

在typescript中在客户端和服务器之间共享代码

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

所以我找到了这篇文章,但没有得到答案,我现在面临着同样的问题,我的项目根目录中有一个客户端、服务器和共享文件夹,客户端和服务器的根目录中都有一个tsconfig,还有一个带有typescript文件的src文件夹,我希望能够像这样导入

从'@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"
  ]
}