javascript - TypeScript 无法解析路径 (2307)
问题描述
我正在构建一个 react-native 应用程序。我在这个问题上花了几个小时,但没有任何帮助。我正在尝试使用 tsconfig 路径来缩短我的长相对导入:
我的 tsconfig 文件:
{
"compilerOptions": {
"allowJs": true,
"allowSyntheticDefaultImports": true,
"baseUrl": "./*",
"esModuleInterop": true,
"isolatedModules": true,
"jsx": "react",
"lib": ["es2017"],
"module": "commonjs",
"moduleResolution": "node",
"noEmit": true,
"paths": {
"@theme/*": ["./src/theme/*"]
},
"strict": true,
"target": "esnext"
},
"include": [
"./src/**/*"
],
"exclude": [
"node_modules"
]
}
我正在尝试按如下方式导入模块:
import { theme } from '@theme'
但是我收到以下错误。此错误在 VSCode 中显示为红色下划线。
Cannot find module '@theme' or its corresponding type declarations.ts(2307)
但是当我运行命令 eslint 时也会出现此错误。
我也尝试将带有 name 属性的 package.json 添加到主题目录,但它也无济于事:
{
"name": "@theme"
}
我使用以下版本:
"typescript": "^4.1.4"
"react-native": "0.63.3",
"react": "16.13.1",
"eslint": "^6.8.0",
Node version: v12.21.0
任何帮助表示赞赏!
解决方案
推荐阅读
- slack - 通过 Slack 斜杠命令打开网页
- javascript - 使用相对路径时导入不起作用。JavaScript
- laravel - 我正在尝试将我的 laravel 项目移至生产环境,但我无法解决此错误 GuzzleHttp\Exception\ConnectException cURL 错误 7
- python-3.x - 我怎样才能将这些 ndarray 之一重塑为这些形式中的任何一种?如果可能,请提供示例代码
- c# - 如何使用 JSONUtility 读取键中带有连字符的对象?
- ios - 使用调度组时如何使公共资源线程安全?
- amazon-web-services - 尝试使用 Github、Docker、ECR 和 EC2 在 AWS 上设置 CI/CD
- javascript - 开发者如何提供没有文件的网站图片(包括背景)?
- reactjs - 在反应中显示日期的一部分
- ios - 即使在从钥匙串访问和 App Store Connect 中删除后,重新启动 Xcode 时,已删除的 iOS 证书仍会继续显示在钥匙串中