How do you use the new “flat config” file (eslint.config.js
) to tell ESLint to only analyze TypeScript files (*.ts
) and to use the TypeScript "strict"
rules?
eslint.config.js
import tseslint from 'typescript-eslint';
export default [
...tseslint.configs.strict,
{
files: ['**/*.ts'],
rules: {
'@typescript-eslint/no-non-null-assertion': 'off',
},
},
];
package.json
"scripts": {
“lint": "eslint --max-warnings 0"
},
I kick off the lint
task with the npm run lint
command, but it fails because it causes ESLint to also analyze the JavaScript files in my project (and output lots of error messages).
Only the TypeScript files (*.ts
) should be analyzed, and they should be analyzed with the "strict"
rules.
Bonus Question:
The official ESLint documentation goes into detail on each configuration option, but I could not find a satisfactory overview of the strategy for selecting files. The “flat config” is an array of objects, but what is the high-level flow for how files are grouped and processed through the objects?
The solution below achieves:
*.js
files (important in my use case)tseslint.config()
wrapper (I will rarely ever edit the configuration file and don't need the IDE help)eslint.config.js
import eslint from '@eslint/js';
import tseslint from 'typescript-eslint';
export default [
eslint.configs.recommended,
...tseslint.configs.strict,
{ ignores: ['**/*.js'] },
{
rules: {
'@typescript-eslint/no-non-null-assertion': 'off',
},
},
];