You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修复Typescript ESLint Indent规则多行对象参数缩进错误

问题描述

我有一段代码:

this.$api
  .get<AxiosResponse<Resource>>('/resources/', {
    params: {
      pageNumber: 1,
      pageLength: 10,
    }
  });

但我的TypeScript ESLint会自动将其格式化为:

this.$api
  .get<AxiosResponse<Resource>>('/resources/', {
  params: {
    pageNumber: 1,
    pageLength: 10,
  }
});

这种格式极易混淆,体验很差。类似问题在@typescript-eslint的相关讨论中已有提及,请问如何更新ESLint配置以避免该问题?

当前ESLint配置如下:

/* eslint-env node */
require('@rushstack/eslint-patch/modern-module-resolution');

module.exports = {
  root: true,
  extends: [
    'plugin:vue/vue3-essential',
    'eslint:recommended',
    '@vue/eslint-config-typescript/recommended',
  ],
  env: {
    'vue/setup-compiler-macros': true,
  },
  overrides: [
    {
      files: ['cypress/integration/**.spec.{js,ts,jsx,tsx}'],
      extends: ['plugin:cypress/recommended'],
    },
  ],
  rules: {
    quotes: [2, 'single', { avoidEscape: true }],
    indent: 'off',
    '@typescript-eslint/no-unused-vars': 'off',
    'vue/max-attributes-per-line': [
      'error',
      {
        singleline: {
          max: 1,
        },
        multiline: {
          max: 1,
        },
      },
    ],
    'vue/html-indent': [
      'error',
      2,
      {
        attribute: 1,
        baseIndent: 1,
        closeBracket: 0,
        alignAttributesVertically: false,
        ignores: [],
      },
    ],
    'vue/html-closing-bracket-newline': [
      'error',
      {
        singleline: 'never',
        multiline: 'always',
      },
    ],
    'vue/script-indent': [
      'error',
      2,
      {
        baseIndent: 0,
        switchCase: 1,
        ignores: [],
      },
    ],
    'vue/attributes-order': [
      'error',
      {
        order: [
          'DEFINITION',
          'LIST_RENDERING',
          'CONDITIONALS',
          'RENDER_MODIFIERS',
          'GLOBAL',
          ['UNIQUE', 'SLOT'],
          'TWO_WAY_BINDING',
          'OTHER_DIRECTIVES',
          'OTHER_ATTR',
          'EVENTS',
          'CONTENT'
        ],
        'alphabetical': false
      }
    ],
    '@typescript-eslint/indent': [
      'error',
      2,
      {
        SwitchCase: 1,
        ObjectExpression: 1,
        MemberExpression: 1,
        CallExpression: {
          arguments: 1,
        },
      }
    ]
  },
};
解决方案

问题根源在于@typescript-eslint/indent规则的配置。当前你设置了CallExpression: { arguments: 1 },该配置会让函数调用的参数从函数名所在的缩进层级开始对齐,但对于带有泛型(<AxiosResponse<Resource>>)的函数调用,ESLint会错误地将泛型后的对象参数对齐到函数名的缩进层级,而非保持与前一行参数的缩进一致。

你需要修改@typescript-eslint/indent规则,添加ignoredNodes配置忽略泛型节点的缩进影响,同时调整CallExpression的参数对齐方式,具体修改如下:

在ESLint配置的rules中,更新@typescript-eslint/indent部分:

'@typescript-eslint/indent': [
  'error',
  2,
  {
    SwitchCase: 1,
    ObjectExpression: 1,
    MemberExpression: 1,
    CallExpression: {
      arguments: 'first' // 改为'first',让参数对齐到第一个参数的起始位置
    },
    ignoredNodes: ['TSTypeParameterInstantiation'] // 忽略泛型声明的缩进检查
  }
]

修改完成后保存配置,重新运行ESLint格式化,代码即可保持你期望的缩进格式。


内容的提问来源于stack exchange,提问作者TheHiggsBroson

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 12:04:57