首页 > 解决方案 > 打字稿项目对共享项目的引用无法解决

问题描述

我正在尝试在 2 个项目之间使用 Typescript 项目引用:client并且sharedclient/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 包失败。

我也试过rootDirclient/tsconfig.json

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

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

有谁知道我做错了什么?

标签: typescriptproject-reference

解决方案


推荐阅读