如何修复ESLINT错误:'默认值'被限制不受我的反应打字条项目中的导出名称

发布于 2025-02-12 20:22:31 字数 758 浏览 0 评论 0原文

我正在更新我的React项目中的ESLINT规则。

目前,我在扩展属性中eslintrc.js

extends: [
    'airbnb',
    'airbnb-typescript',
    'airbnb/hooks',
    // "plugin:@typescript-eslint/recommended",
    // "plugin:@typescript-eslint/recommended-requiring-type-checking",
    // "plugin:eslint-comments/recommended",
    'plugin:react/recommended',
    'plugin:jest/recommended',
    'plugin:prettier/recommended',
  ],

我遇到此错误:

错误'默认值'被限制被用作导出名称 无限制的出口

我们对组件的模式是这样的:

Button/
 - Button.tsx
 - Button.spec.ts
 - Button.stories.tsx
 - index.ts

index.ts:

从'./button'export {default} export {default};

如何解决这个问题?还是我必须以某种方式覆盖这个Eslint规则?

I am updating eslint rules in my React project.

Currently I have this in the extend property inside eslintrc.js:

extends: [
    'airbnb',
    'airbnb-typescript',
    'airbnb/hooks',
    // "plugin:@typescript-eslint/recommended",
    // "plugin:@typescript-eslint/recommended-requiring-type-checking",
    // "plugin:eslint-comments/recommended",
    'plugin:react/recommended',
    'plugin:jest/recommended',
    'plugin:prettier/recommended',
  ],

I am getting this error:

error 'default' is restricted from being used as an exported name
no-restricted-exports

Our pattern for components is like this:

Button/
 - Button.tsx
 - Button.spec.ts
 - Button.stories.tsx
 - index.ts

index.ts:

export { default } from './Button';

How to fix this? Or do I have to override this eslint rule somehow?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

初心未许 2025-02-19 20:22:32

您需要禁用此规则 - 无限制的exports
您的.eslintrc文件将在这里看起来像这样

{
  "extends": [
    'airbnb',
    'airbnb-typescript',
    'airbnb/hooks',
    // "plugin:@typescript-eslint/recommended",
    // "plugin:@typescript-eslint/recommended-requiring-type-checking",
    // "plugin:eslint-comments/recommended",
    'plugin:react/recommended',
    'plugin:jest/recommended',
    'plugin:prettier/recommended',
  ],
  "rules": {
    "no-restricted-exports": 0,
  }
}

的文档 - htttps:htttps:// eslint。 org/doc/最新/规则/无限制的exports

另一个选项是不使用默认导出,而是执行这样的操作:

export const Button = () => {...};

在index.ts文件中:

export * from './Button';

You'll need to disable this rule - no-restricted-exports.
Your .eslintrc file will look like this

{
  "extends": [
    'airbnb',
    'airbnb-typescript',
    'airbnb/hooks',
    // "plugin:@typescript-eslint/recommended",
    // "plugin:@typescript-eslint/recommended-requiring-type-checking",
    // "plugin:eslint-comments/recommended",
    'plugin:react/recommended',
    'plugin:jest/recommended',
    'plugin:prettier/recommended',
  ],
  "rules": {
    "no-restricted-exports": 0,
  }
}

More documentation here - https://eslint.org/docs/latest/rules/no-restricted-exports.

Another option would be to not use default exports but instead do something like this:

export const Button = () => {...};

and in the index.ts file:

export * from './Button';
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文