简体   繁体   English

Typescript 抱怨 React Native 中的绝对导入

[英]Typescript complaining about Absolute imports in React Native

So Im trying to implement Absolute imports using JSON files in directories.所以我尝试使用目录中的 JSON 文件来实现绝对导入。

In JSON: {"name": "components"}在 JSON: {"name": "components"}

I have specified paths in config JSON, but still not working, I have tried with baseURL and without, with "/*" at the end:我已经在配置 JSON 中指定了路径,但仍然无法正常工作,我尝试使用 baseURL 和不使用,最后使用“/*”:

"paths": {
      "components/*":["./src/components/*"]
    },```

and without:

"paths": {
      "components":["./src/components"]
    },

Code is working normally, only problem is TS error.代码工作正常,唯一的问题是 TS 错误。

This is how I usually do for my projects.这就是我通常为我的项目所做的。

yarn add -D babel-plugin-module-resolver

babel.config.js babel.config.js

module.exports = {
  presets: ['module:metro-react-native-babel-preset'],
  plugins: [
    [
      'module-resolver',
      {
        extensions: ['.ios.js', '.android.js', '.js', '.ts', '.tsx', '.json'],
        alias: {
          'components': './src/components',
        },
      },
    ],
  ],
};

tsconfig.json tsconfig.json

{
  "extends": "@tsconfig/react-native/tsconfig.json",
  "compilerOptions": {
    "skipLibCheck": true,

    /* for module resolver */
    "baseUrl": ".",
    "paths": {
      "components/*": ["src/components/*"]
    }
  }
}

Restart VSCode重启 VSCode

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM