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

已设置singleQuote:true仍遇Prettier单引号转双引号报错,如何解决?

解决Prettier强制转换带转义单引号字符串的问题

Prettier的singleQuote: true规则虽然优先使用单引号,但碰到字符串内部包含转义单引号(比如'foo \'n bar')时,它会判定用双引号包裹更简洁(无需转义),因此不管你ESLint里怎么配置单引号选项,都会触发格式化报错。

要解决这个问题,只需给Prettier添加avoidEscape: true配置:

方案1:修改ESLint配置

直接在eslint.config.ts的prettier/prettier规则中新增该选项:

'prettier/prettier': [
  'error',
  {
    singleQuote: true,
    jsxSingleQuote: true,
    avoidEscape: true // 关键配置
  }
]

方案2:使用独立Prettier配置文件

如果项目有单独的.prettierrc或prettier.config.js,直接添加该选项:

{
  "singleQuote": true,
  "jsxSingleQuote": true,
  "avoidEscape": true
}

这个配置的作用是告诉Prettier:当单引号字符串内部包含转义的单引号时,不要强制转换成双引号,保留原有的单引号写法。修改完成后重新运行格式化,'foo \'n bar'就不会再被报错要求转换了。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.13 09:47:05