typescript-eslint-converter v1.2.0
TypeScript ESLint Converter
Automatic ESLint rule conversions for TypeScript.
ESLint replaces TSLint for linting TypeScript.
Existing JavaScript rules will be converted to support TypeScript, so you can combine this with base configurations such as airbnb easily. See below for full details.
Installation
This assumes you have already installed and configured ESLint.
npm install --save-dev typescript-eslint-converterChange your .eslintrc.js:
const typescriptEslintConverter = require('typescript-eslint-converter');
module.exports = typescriptEslintConverter({
// existing configuration here; for example airbnb:
extends: ['airbnb'],
});This project is not limited to airbnb! You can use any ESLint configuration, and it will be converted to be TypeScript-compatible (see below for full details).
Note that by default, indent is not converted to @typescript-eslint/indent (due to
typescript-eslint#1824).
If you want to enable indentation linting despite the known issues, you can:
const typescriptEslintConverter = require('typescript-eslint-converter');
module.exports = typescriptEslintConverter({
// existing configuration here
}, {
indent: true, // enable indent -> @typescript-eslint/indent conversion
});Customisation
Adding or customising TypeScript-specific rules
The recommended way to add or customise TypeScript rules is with an override. This prevents
ESLint attempting to apply the rules to Javascript files:
const typescriptEslintConverter = require('typescript-eslint-converter');
module.exports = typescriptEslintConverter({
extends: ['airbnb'], /* or whatever you are using */
overrides: [
{
files: ['*.ts', '*.tsx'],
rules: {
// examples:
// use airbnb quote rules for JS, but backticks for TS:
'@typescript-eslint/quotes': ['error', 'backtick'],
// TS-specific rule: enforce Array<T> rather than T[]
'@typescript-eslint/array-type': ['error', 'generic'],
},
}
],
});Options
By default, ts and tsx files will be handled as TypeScript. You can customise this if needed:
const typescriptEslintConverter = require('typescript-eslint-converter');
module.exports = typescriptEslintConverter({
/* existing configuration here */
}, {
// default values are shown:
typescriptFiles: ['*.ts', '*.tsx'],
resolveExtensions: ['js', 'mjs', 'jsx', 'mjsx', 'ts', 'tsx'],
autoParseResolvableExtensions: true,
useLoaderStyle: null,
recommended: true,
indent: false,
});typescriptFilescontrols the pattern used to identify a file as TypeScript when overriding rules.resolveExtensionslists all file extensions which should be recognised byimport/resolver.autoParseResolvableExtensionsenables emptyoverridesfor all entries inresolveExtensions; this means matching files will be linted without needing to specify--exton the CLI. If you do not want this behaviour, you can set it tofalse(all entries intypescriptFileswill continue to be linted automatically). Note that this feature only works with ESLint 7+.useLoaderStyleiftrue, forcesbaseConfig-style configuration (used by theCLIEngineAPI andeslint-loader). Iffalse, forces flat behaviour (matching.eslintrcfiles). By default, this will automatically detect the presence ofbaseConfigin the input configuration. When using inside a.eslintrcfile, you should not need to change this. If using in a wrapper project (such asneutrino), you may need to set this totrueto guarantee correct behaviour.recommendedadds'plugin:@typescript-eslint/recommended'to theextendsoption. If you do not want this, set it tofalse.indentconverts any existingindentrule to@typescript-eslint/indent. This is disabled by default due to known issues with@typescript-eslint/indent.
Automatic rule conversion
Several rules are automatically converted. If you believe another rule should be automatically converted, please raise an issue.
Global rule changes
react/jsx-filename-extensionextensionswill havetsandtsxadded to mirrorjsandjsx.
import/no-extraneous-dependencies- Any
devDependenciesglob patterns will be extended to include.ts*if they contain.js*.
- Any
import/extensions.ts*equivalents for all.js*extensions will be added.
TypeScript file rule changes
These rule changes only apply to .ts and .tsx source files:
Disable all rules which are checked by the TypeScript compiler:
getter-returnno-dupe-argsno-dupe-keysno-unreachablevalid-typeof&babel/valid-typeofno-const-assignno-new-symbolno-this-before-superno-undefno-dupe-class-membersno-redeclare
Convert native ESLint and babel rules which do not support TypeScript: (any configuration is copied over; the TypeScript rules are config-compatible)
brace-style→@typescript-eslint/brace-stylecomma-spacing→@typescript-eslint/comma-spacingdefault-param-last→@typescript-eslint/default-param-lastdot-notation→@typescript-eslint/dot-notation- The TypeScript rule offers additional configuration options
func-call-spacing→@typescript-eslint/func-call-spacinginit-declarations→@typescript-eslint/init-declarationskeyword-spacing→@typescript-eslint/keyword-spacinglines-between-class-members→@typescript-eslint/lines-between-class-members- The TypeScript rule offers additional configuration options
no-array-constructor→@typescript-eslint/no-array-constructorno-dupe-class-members→@typescript-eslint/no-dupe-class-membersno-empty-function→@typescript-eslint/no-empty-functionno-extra-parens→@typescript-eslint/no-extra-parensno-extra-semi→@typescript-eslint/no-extra-semino-invalid-this&babel/no-invalid-this→@typescript-eslint/no-invalid-thisno-loop-func→@typescript-eslint/no-loop-funcno-loss-of-precision→@typescript-eslint/no-loss-of-precisionno-magic-numbers→@typescript-eslint/no-magic-numbers- The TypeScript rule offers additional configuration options
no-redeclare→@typescript-eslint/no-redeclare- The TypeScript rule offers additional configuration options
no-shadow→@typescript-eslint/no-shadow- The TypeScript rule offers additional configuration options
no-unused-expressions&babel/no-unused-expressions→@typescript-eslint/no-unused-expressionsno-unused-vars→@typescript-eslint/no-unused-varsno-use-before-define→@typescript-eslint/no-use-before-define- The TypeScript rule offers additional configuration options
no-useless-constructor→@typescript-eslint/no-useless-constructorquotes&babel/quotes→@typescript-eslint/quotesrequire-await→@typescript-eslint/require-awaitno-return-await→@typescript-eslint/return-await- The TypeScript rule offers additional configuration options
- The default
in-try-catchmatchesno-return-await's behaviour
semi&babel/semi→@typescript-eslint/semispace-before-function-paren→@typescript-eslint/space-before-function-paren
indent- This rule is disabled by default due to typescript-eslint#1824.
- If you want to enable indentation linting, use the
indentoption (described above).