网站搜索

TypeScript 项目引用共享项目无法解析


我正在尝试在两个项目之间使用 TypeScript 项目引用:client 和 shared,client/tsc -b 进程成功了。但是,在运行时导入 shared 文件失败。

项目结构非常简单:

./client
./client/tsconfig.json
./client/src
./client/src/app-load.tsx
./shared
./shared/tsconfig.json
./shared/src
./shared/src/validation-utils.ts

./client/tsconfig.json 的内容

{
    "compilerOptions": {
        "module": "es2015",
        "target": "es2015",
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true,
        "moduleResolution": "node",
        "jsx": "react",
        "outDir": "build",
        "rootDir": "src",
        "baseUrl": "./",
        "allowSyntheticDefaultImports": true,
        "preserveConstEnums": true,
        "sourceMap": true,
        "skipLibCheck": true,
        "types": ["react", "react-native", "jest"],
        "paths": {
      "@shared/*": [
        "../shared/src/*"
      ]
    }
    },
    "compileOnSave": false,
    "exclude": ["android", "App.js", "ios", "build", "node_modules"],
    "references": [{ 
        "path": "../shared"
    }]
}

./shared/tsconfig.json 的内容

{
  "compilerOptions": {
    "module": "es2015",
    "target": "es2015",
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "rootDir": "src",
    "outDir": "build",
    "baseUrl": "src",
    "composite": true,
    "declaration": true,
    "declarationMap": true,
  },
  "references": []
}

在 ./client/src/app-load.tsx 中使用的导入

import { isEmailValid } from "@shared/validation-utils";

运行时错误消息

无法从 "build/app-load.js" 解析 "@shared/validation-utils" 构建 JavaScript 包失败。

我也尝试过在 client/tsconfig.json 中替换 rootDir 为

"include": [
  "src/**/*",
  "../shared/**/*"
],

就像在其他问题中看到的那样,仍然没有成功。

有人知道我做错了什么吗?