我正在尝试为我的 React Native 项目中的包配置 tsconfig.json 以添加模块解析并使代码更清晰,而不是
所以文件夹结构是这样的:
root
--src
--packages
---src
----components
----constants
---tsconfig.json
import { LABELS } from "../../../constants";
我可以写
import { LABELS } from "src/constants"; or import { LABELS } from "@app-content/constants";
这是我的 tsconfig.json
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@app-content/*": ["./src/*"]
}
},
"include": ["__tests__/**/*", "src/**/*", "typings/*"]
}
如果我写“src/”或“@app-content/”,该分辨率在 vs-code 上可以正常工作,它会显示正确的文件夹,并且当时没有错误,应用程序也运行成功,但是当我运行命令
tsc
它给出错误
error TS2307: Cannot find module 'src/constants' or its corresponding type declarations.
我想我以前也遇到过这个问题,你检查过运行编译器的目录吗?