typescript-eslint
Tooling which enables you to use TypeScript with ESLint
This package is the main entrypoint that you can use to consume our tooling with ESLint.
This package exports the following:
Installationyarn add typescript-eslint
pnpm add typescript-eslint
Usage
We recommend getting started by using the tseslint.config()
helper function in your ESLint config:
eslint.config.mjs
import eslint from '@eslint/js';
import tseslint from 'typescript-eslint';
export default tseslint.config(
eslint.configs.recommended,
tseslint.configs.recommended,
);
This config file exports a flat config that enables both the core ESLint recommended config and our recommended config.
note
ESLint also provides a defineConfig()
helper similar to tseslint.config()
. However, there is a types incompatibility issue that causes type errors to incorrectly be reported when mixing typescript-eslint's configs and defineConfig()
. For now we recommend using tseslint.config()
for use with typescript-eslint configs.
See typescript-eslint#10899 for more details.
config(...)
tseslint.config(...)
takes in any number of ESLint config objects, each of which may additionally include an extends
array of configs to extend. tseslint.config(...)
returns the equivalent ESLint config of applying the rest of the settings for each extension.
By using this function you will get autocomplete and documentation for all config properties. Additionally, if you provide invalid values, it can trigger informative TypeScript type errors.
eslint.config.mjs
import eslint from '@eslint/js';
import tseslint from 'typescript-eslint';
export default tseslint.config(
eslint.configs.recommended,
tseslint.configs.recommended,
{
},
);
eslint.config.mjs
import eslint from '@eslint/js';
import tseslint from 'typescript-eslint';
export default [
eslint.configs.recommended,
...tseslint.configs.recommended,
{
},
];
note
We strongly recommend using this utility to improve the config authoring experience — however it is entirely optional. By choosing not to use it you lose editor autocomplete and type checking for config files. Otherwise it will not impact your ability to use our tooling.
Flat configextends
The tseslint.config()
utility function also adds handling for the extends
property on flat config objects. This allows you to more easily extend shared configs for specific file patterns whilst also overriding rules/options provided by those configs:
export default tseslint.config({
files: ['**/*.ts'],
extends: [
eslint.configs.recommended,
tseslint.configs.recommended,
],
rules: {
'@typescript-eslint/array-type': 'error',
},
});
export default [
...[
eslint.configs.recommended,
...tseslint.configs.recommended,
].map(conf => ({
...conf,
files: ['**/*.ts'],
})),
{
files: ['**/*.ts'],
rules: {
'@typescript-eslint/array-type': 'error',
},
},
];
We found that this is a common operation when writing ESLint configs which is why we provided this convenience property.
For example, in codebases with type-aware linting, a config object like the following is a common way to disable TypeScript-specific linting setups on JavaScript files:
export default tseslint.config({
files: ['**/*.js'],
extends: [tseslint.configs.disableTypeChecked],
rules: {
'other-plugin/typed-rule': 'off',
'@typescript-eslint/explicit-function-return-type': 'off',
},
});
Manual usage
typescript-eslint's recommended and stylistic configurations specify typescript-eslint parser
and plugin
options for you, so there is no need to manually provide those. However, in complex ESLint configurations, you may find yourself manually specifying those options yourself.
You can declare our plugin and parser in your config via this package, for example:
eslint.config.mjs
import eslint from '@eslint/js';
import jestPlugin from 'eslint-plugin-jest';
import tseslint from 'typescript-eslint';
export default tseslint.config({
plugins: {
'@typescript-eslint': tseslint.plugin,
},
languageOptions: {
parser: tseslint.parser,
parserOptions: {
projectService: true,
tsconfigRootDir: import.meta.dirname,
},
},
rules: {
'@typescript-eslint/no-floating-promises': 'error',
},
});
Usage with other plugins
Below is a more complex example of how you might use our tooling with flat configs. This config:
build
/dist
folders from being lintedeslint-plugin-jest
rules on test files onlyeslint.config.mjs
import eslint from '@eslint/js';
import jestPlugin from 'eslint-plugin-jest';
import tseslint from 'typescript-eslint';
export default tseslint.config(
{
ignores: ['**/build/**', '**/dist/**', 'src/some/file/to/ignore.ts'],
},
eslint.configs.recommended,
{
plugins: {
'@typescript-eslint': tseslint.plugin,
jest: jestPlugin,
},
languageOptions: {
parser: tseslint.parser,
parserOptions: {
projectService: true,
},
},
rules: {
'@typescript-eslint/no-floating-promises': 'error',
},
},
{
files: ['**/*.js'],
extends: [tseslint.configs.disableTypeChecked],
},
{
files: ['test/**'],
extends: [jestPlugin.configs['flat/recommended']],
},
);
Migrating from legacy .eslintrc
configs
If you're migrating from a legacy .eslintrc
configuration setup you likely have our plugin and parser installed separately. This package includes these as dependencies so you can freely uninstall your local references:
npm un @typescript-eslint/parser @typescript-eslint/eslint-plugin
yarn remove @typescript-eslint/parser @typescript-eslint/eslint-plugin
pnpm remove @typescript-eslint/parser @typescript-eslint/eslint-plugin
For more information on migrating from a "legacy" config setup, see ESLint's Configuration Migration Guide.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4