首页 > 解决方案 > 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

任何帮助表示赞赏!

标签: javascriptnode.jstypescriptreact-nativevisual-studio-code

解决方案


推荐阅读