【问题标题】:StyledComponents + Typescript样式化组件 + 打字稿
【发布时间】:2021-06-07 16:38:27
【问题描述】:

我在 react app + typescript 中遇到了 eslint 的奇怪问题。我正在使用样式化组件,我需要为我的组件创建自定义道具界面,我遵循文档,当我像文档中的示例一样编写它时,我得到了这个 eslint 错误?有人知道这里发生了什么吗?

*eslint gives error here StyledMenuButtonProps is defined but never used*
interface StyledMenuButtonProps {
  opened: boolean;
}

*and here the error is StyledMenuButtonProps is not defined*
const StyledMenuButtonWrapper = styled.div<StyledMenuButtonProps>`
  width: 40px;
  height: 20px;
  position: relative;
  margin: 0 20px;

我的 .eslintrc.js 文件:

module.exports = {
  env: {
    browser: true,
    es2020: true,
  },
  extends: ['plugin:react/recommended', 'airbnb', 'prettier'],
  parser: '@typescript-eslint/parser',
  parserOptions: {
    ecmaFeatures: {
      jsx: true,
    },
    ecmaVersion: 2020,
    sourceType: 'module',
  },
  plugins: ['react', '@typescript-eslint', 'prettier'],
  rules: {
    'react/jsx-filename-extension': [1, { extensions: ['.js', '.jsx', '.ts', '.tsx'] }],
    'react/prop-types': 'off',
    'no-unused-vars': 2,
    '@typescript-eslint/ban-types': 'off',
    '@typescript-eslint/explicit-function-return-type': [0],
    'no-use-before-define': 'off',
    'import/prefer-default-export': 'off',
    'import/no-default-export': 'error',
    'import/no-unresolved': 'off',
    'import/extensions': 'off',
    'consistent-return': 'off',
  },
  settings: {
    react: {
      version: 'detect',
    },
    'import/resolver': {
      node: {
        extensions: ['.ts', '.tsx', '.js', '.jsx'],
      },
    },
  },
};

eslint 的 devDependencies:

"@typescript-eslint/eslint-plugin": "^4.17.0",
"@typescript-eslint/parser": "^4.17.0",
"eslint": "^7.21.0",
"eslint-config-airbnb": "^18.2.1",
"eslint-config-prettier": "^8.1.0",
"eslint-plugin-import": "^2.22.1",
"eslint-plugin-jsx-a11y": "^6.4.1",
"eslint-plugin-prettier": "^3.3.1",
"eslint-plugin-react": "^7.22.0",
"eslint-plugin-react-hooks": "^4.2.0",

使用最新的 CRA 和 React 脚本。

谢谢

【问题讨论】:

  • 您可以编辑您的问题以包含您共享的代码 sn-p,而不是分享您的代码图片吗?
  • 好的,来了
  • 如果你分享你当前的 linter 配置文件也很棒
  • 对不起,在这里!

标签: reactjs typescript interface styled-components


【解决方案1】:

这个问题在这里得到了回答:https://stackoverflow.com/a/55439681/10490197 :)

基本上,您使用的规则无法正确理解 TypeScript,因此您需要修复它:

"rules": {
  "no-unused-vars": "off",
  "@typescript-eslint/no-unused-vars": "error"
}

【讨论】:

  • 将此添加到我的规则对象中,但我的代码仍然在样式化组件上大喊 eslint 错误:未定义 StyledMenuButtonProps,即使它在上面定义了
猜你喜欢
  • 2020-08-02
  • 2020-06-27
  • 2019-06-19
  • 1970-01-01
  • 2021-03-04
  • 2018-06-06
  • 2020-01-22
  • 2021-03-13
  • 2018-07-17
相关资源
最近更新 更多