【发布时间】:2022-06-20 23:48:29
【问题描述】:
我正在尝试在我的tsconfig.json 中为与 Vite 捆绑的 React 应用程序设置路径别名。这是我tsconfig.json的相关部分:
{
"compilerOptions": {
"baseUrl": ".",
...
"paths": {
"*": ["src/*", "node_modules/*"],
"components/*": ["src/components/*"],
"containers/*": ["src/containers/*"],
"pages/*": ["src/constants/*"],
"store/*": ["src/store/*"],
"types/*": ["src/types/*"],
"NestedFolder/*": [
"src/components/NestedFolder/*"
],
}
},
"include": ["src/**/*", "*"]
}
唯一的问题是NestedFolder。当我以这种方式导入时,一切正常:
import { ComponentName } from "components/NestedFolder/types";
但是,嵌套别名失败:
import { ComponentName } from "NestedFolder/types";
// error
EslintPluginImportResolveError: typescript with invalid interface loaded as resolver
Occurred while linting .../src/components/NestedFolder/canvas/index.ts:1
Rule: "import/namespace"
// error on hover in VS Code
Unable to resolve path to module 'NestedFolder/types'.eslintimport/no-unresolved
我想做嵌套组件,因为我有几个嵌套 3-4 层的文件夹,如果我的导入有一个更清晰的视图会很好。有没有办法做到这一点?
【问题讨论】:
-
如果您使用的是 VSCode,我假设您已尝试重新启动 TS 服务器。我建议使用vite-tsconfig-paths plugin 看看是否可行
-
我重新启动了我的服务器 VS Code,并运行了构建命令,构建中断。谢谢,我去看看插件。我希望避免添加额外的库/插件,但这可能是我现在唯一的选择。
-
更新:插件确实解决了我的问题,谢谢!
-
没问题!我会将我的评论作为答案,以便其他可能需要找到此插件的人更清楚
标签: reactjs typescript vite