Vincas Stonys
Vincas Stonys

Reputation: 1135

How to use the same rules for js and ts in TSLint

I want to use the same style in .js files and .ts files. I know there is jsRules property in tslint.json, but I see 2 problems with it:

Any other way to have the same code style without having to maintain both eslint and tslint?

Upvotes: 13

Views: 1935

Answers (1)

Karol Majewski
Karol Majewski

Reputation: 25790

Not everyone knows that, but TSLint configuration doesn't have to come in a .json file. You can use the .js extension, and keep whatever logic you like inside.

In this case, we can split TSLint rules into two categories — universal rules and React-specific ones. Both rulesets will be used for TypeScript files, but only the first ones will be applied to JavaScript.

tslint.js

const UNIVERSAL_RULES = {
  "max-line-length": [true, 120]
}

const REACT_RULES = {
  "jsx-space-before-trailing-slash": true
}

module.exports = {
  extends: [
    "tslint:recommended"
  ],
  jsRules: UNIVERSAL_RULES,
  rules: {
    ...UNIVERSAL_RULES,
    ...REACT_RULES
  }
}

When running TSLint CLI, it may be required to point to your configuration file explicitly. You can do that by adding --config tslint.js to your tslint command.

Upvotes: 5

Related Questions