首页 > 解决方案 > 如何制作别名以指向外部文件夹?

问题描述

我有一个 React 项目,我想在其中使用一些别名来引用项目外部文件夹中的一些代码。这些文件夹没有 package.json 也没有 tsconfig.json,只有 .ts 文件。

这是我的 tsconfig.json:

{
  "compilerOptions": {
      "outDir": "./dist/",        // path to output directory
      "sourceMap": true,          // allow sourcemap support
      "strictNullChecks": true,   // enable strict null checks as a best practice
      "module": "esnext",         // specify module code generation
      "jsx": "react",             // use typescript to transpile jsx to js
      "target": "es5",            // specify ECMAScript target version
      "allowJs": true,             // allow a partial TypeScript and JavaScript codebase
      "baseUrl": ".",
      "esModuleInterop": true,
      "allowSyntheticDefaultImports": true,
      "moduleResolution": "node",
      "resolveJsonModule": true,
      "isolatedModules": true,
      "paths": {
        "@Root/*": ["./src/*"],
        "@Common/*": ["./../common/*"],
        "@Images/*": ["./../common/assets/images/*"],
    }
  },
  "include" : ["./src/", "./../common/"],
  "exclude": ["node_modules", "dist", "config", ".vscode"]
}

但是,每次我使用位于项目外部的“common”文件夹中的 @Common 或 @Images 引用某些内容时,我都会从 VSCode 中得到一个红色突出显示。

预先感谢您和问候

标签: reactjstypescriptwebpacktsconfig

解决方案


推荐阅读