首页 > 解决方案 > 您可以为根目录之外的引用设置路径别名吗

问题描述

我正在开发一个具有以下目录结构的打字稿项目

├── common
│   ├── tsconfig.json
│   ├── Schemas
│   └── Types
├── server
│   ├── tsconfig.json
│   ├── src
│   └── test
└── viewer
    ├── tsconfig.json
    ├── src
    └── tests

我已经以tsconfig.json这种方式成功设置了对服务器和查看器中公共目录的引用

"references": [
    { "path": "../common" }
]

所以我可以包含公共文件

import EventListSchema from '../../../../common/Schemas/EventList';

但是,我想像我一样设置一个路径别名src

"paths": {
    "@/*": [ "src/*" ],
    "common/*": [ "../common/*" ] // something like this
},

所以我可以包含具有更简单路径的常见文件,例如

import EventListSchema from 'common/Schemas/EventList';

当我尝试添加时common出现paths此错误

error TS2307: Cannot find module 'common/Schemas/EventList' or its corresponding type declarations.

import EventListSchema from 'common/Schemas/EventList';

这样的事情可能吗?我正在使用tsc版本 4.0.2

标签: typescriptconfiguration

解决方案


推荐阅读