How to tell eslint that you prefer single quotes around your strings

I'm new to eslint and it's spewing out a ton of errors telling me to use doublequotes:

error  Strings must use doublequote

That's not my preference. I've got an .eslintrc file set up with the basics:

{
  "env": {
    "node": 1
  }
}

I'd like to configure it for single quotes.


Solution 1:

http://eslint.org/docs/rules/quotes.html

{
  "env": {
    "node": 1
  },
  "rules": {
    "quotes": [2, "single", { "avoidEscape": true }]
  }
}

Solution 2:

If you're using TypeScript/ES6 you might want to include template literals (backticks). This rule prefers single quotes, and allows template literals.

TypeScript Examples

"@typescript-eslint/quotes": [
  "error",
  "single",
  {
    "allowTemplateLiterals": true
  }
]

Another useful option is to allow single or double quotes as long as the string contains an escapable quote like "lorem ipsum 'donor' eta" or 'lorem ipsum "donor" eta'.

"@typescript-eslint/quotes": [
  "error",
  "single",
  {
    "avoidEscape": true,
    "allowTemplateLiterals": true
  }
]

References:

ESLint

https://eslint.org/docs/rules/quotes

TypeScript ESLint

https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/quotes.md

Solution 3:

rules: {
  'prettier/prettier': [
    'warn',
    {
      singleQuote: true,
      semi: true,
    }
  ],
},

In version "eslint": "^7.21.0"

Solution 4:

For jsx strings, if you would like to set this rule for all files, create the rule in the eslint config file.

  rules: {
    'jsx-quotes': [2, 'prefer-single'],
  }

Or 'prefer-double' for double quotes.