简体   繁体   English

使用 babel 模块解析器和 typescript 时的 ESLint 问题

[英]ESLint issue when using babel module-resolver and typescript

TS is not inferring the type when using resolved imports, but it does when using relative imports. TS 在使用解析导入时不推断类型,但在使用相对导入时会推断类型。 Any help would be appreciated.任何帮助,将不胜感激。

发现.tsx

useTheme has使用主题有

"Unsafe call of an 'any' typed value." “对‘任意’类型值的不安全调用。”

This error does not show up if i use a relative import instead of '@theme/Theme'如果我使用相对导入而不是'@theme/Theme' ,则不会出现此错误

使用主题

My project structure is:我的项目结构是:

项目结构

Here are my config files:这是我的配置文件:

tsconfig.json tsconfig.json

{
  "extends": "../../tsconfig.json", // standard tsconfig params
  "compilerOptions": {
    "jsx": "react-native" /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */,
    "baseUrl": "." /* Base directory to resolve non-absolute module names. */,
    "paths": {
      "@components/*": ["./src/common/components/*"],
      "@theme/*": ["./src/lib/theme/*"],
      "@hooks/*": ["./src/common/hooks/*"],
      "@features/*": ["./src/features/*"]
    },
    "typeRoots": [
      "./typings",
      "../../node_modules/@types"
    ] /* List of folders to include type definitions from. */
  },
  "include": ["../"],
  "exclude": [
    "node_modules",
    "babel.config.js",
    "metro.config.js",
    "jest.config.js"
  ]
}

babel.config.js babel.config.js

module.exports = {
  presets: [
    'module:metro-react-native-babel-preset',
    '@babel/preset-typescript',
  ],
  plugins: [
    ['@babel/plugin-proposal-optional-chaining'],
    ['@babel/plugin-proposal-nullish-coalescing-operator'],
    ['@babel/plugin-proposal-decorators', { legacy: true }],
    [
      'module:react-native-dotenv',
      {
        moduleName: '@env',
        path: '.env',
      },
    ],
    [
      'module-resolver',
      {
        alias: {
          '@components': './src/common/components',
          '@theme': ['./src/lib/theme'],
          '@hooks': ['./src/common/hooks'],
          '@features': ['./src/features'],
        },
        extensions: ['.js', '.jsx', '.ts', '.tsx'],
        root: '.',
      },
    ],
    'react-native-reanimated/plugin',
  ],
};

.eslintrc .eslintrc

{
  "root": true,
  "extends": [
    "airbnb-typescript", // React "airbnb-typescript/base" for just js. "airbnb","airbnb/base" for no TypeScript
    "airbnb/hooks", // React
    "plugin:@typescript-eslint/eslint-recommended",
    "plugin:@typescript-eslint/recommended",
    "plugin:@typescript-eslint/recommended-requiring-type-checking",
    "@react-native-community" // React Native only
  ],
  "parser": "@typescript-eslint/parser", // TypeScript only
  "parserOptions": {
    "project": "./tsconfig.json", // TypeScript only
    "ecmaFeatures": { "jsx": true }
  },
  "settings": {
    "import/resolver": {
      "babel-module": {}
    }
  },
  "plugins": ["import", "@typescript-eslint"],
  "ignorePatterns": ["*.config.js"],
  "overrides": [
    {
      /** Testing Files Extensions */
      "files": ["*.{spec,test}.{js,ts,tsx}", "e2e/*.{js,ts,tsx}"],
      "plugins": ["jest"],
      "env": { "jest/globals": true },
      "rules": {
        "import/no-extraneous-dependencies": 0 // Allow dev-dependency imports
      }
    }
  ],
  "rules": {
    "no-unused-vars": [
      "error",
      {
        "args": "none",
        "caughtErrors": "none",
        "ignoreRestSiblings": true,
        "vars": "all"
      }
    ],
    // resolve "import/extensions"
    "import/extensions": [
      "error",
      "ignorePackages",
      {
        "ts": "never",
        "tsx": "never"
      }
    ],
    "curly": ["error", "multi-line"],
    "no-void": ["error"], // Allow void as statement in order to ignore promise returns
    "prettier/prettier": 0, // Disable Prettier
    "import/no-cycle": 0, // Slows down all linting
    "react/jsx-props-no-spreading": 0, // Enable Prop Spreading
    "@typescript-eslint/no-empty-interface": 0, // Allow boilerplate empty interfaces for defining component props explicitly
    "@typescript-eslint/no-unsafe-assignment": 0,
    "@typescript-eslint/naming-convention": [
      2,
      {
        "leadingUnderscore": "allow",
        "format": ["camelCase", "PascalCase", "UPPER_CASE", "snake_case"],
        "selector": "variable"
      }
    ],
    "@typescript-eslint/no-explicit-any": 0, // disabled to avoid using any type
    "@typescript-eslint/no-use-before-define": "off",
    "@typescript-eslint/restrict-template-expressions": 0,
    "@typescript-eslint/no-floating-promises": [2, { "ignoreIIFE": true }], // Fixes issue where async await syntax wasn't being recognised.
    "@typescript-eslint/explicit-function-return-type": [
      0,
      { "allowExpressions": false }
    ],
    "@typescript-eslint/no-unused-vars": [2, { "varsIgnorePattern": "^_" }],

    // Fixes false-positives for enums in typescript https://github.com/typescript-eslint/typescript-eslint/issues/2484#issuecomment-687257773
    "no-shadow": "off",
    "@typescript-eslint/no-shadow": ["error"],
    "max-len": [
      "warn",
      {
        "code": 120,
        "tabWidth": 2,
        "ignoreComments": true,
        "ignoreStrings": true,
        "ignoreRegExpLiterals": true,
        "ignoreTemplateLiterals": true
      }
    ]
  }
}

if you work in vscode, try use "TypeScript: Select TypeScript version" to select you workspace version如果你在 vscode 中工作,请尝试使用“TypeScript:Select TypeScript 版本”到 select 你的工作区版本

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

相关问题 Babel 模块解析器不适用于 Mocha 测试 - Babel module-resolver not working with Mocha tests React Native Typescript babel-plugin-module-resolver 错误:找不到模块或其对应的类型声明 - React Native Typescript babel-plugin-module-resolver error: Cannot find module or its corresponding type declerations Eslint 在使用 babel 解析器时禁用严格模式 - Eslint disable strict mode when using babel parser Babel 模块解析器不适用于 react-native - Babel module resolver not working with react-native 使用样式组件和 Material UI 时的 Typescript 和 eslint 问题:`React 无法识别 DOM 元素上的 `showText` 属性 - Typescript and eslint issue when using styled-components and Material UI: `React does not recognize the `showText` prop on a DOM element` 使用create-react-app时修改模块解析器 - Modify module resolver when using create-react-app 与模块订单有关的EsLint问题 - EsLint issue related to module order 使用 Babel 时 React 标记库出现问题 - Issue with marked library with React when using Babel React Native-未定义的babel-plugin-module-resolver'1' - React native - babel-plugin-module-resolver '1' of undefined 如何在 babel-plugin-module-resolver 中使用 root 选项? - How to use root option in babel-plugin-module-resolver?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM