The defineConfig helper provides type safety and IDE autocompletion when defining ESLint flat configuration files.
Import
const { defineConfig } = require ( "eslint/config" );
// or ES modules
import { defineConfig } from "eslint/config" ;
Usage
Basic Usage
const { defineConfig } = require ( "eslint/config" );
module . exports = defineConfig ({
rules: {
"no-console" : "error" ,
"no-var" : "warn"
}
});
With TypeScript
The primary benefit of defineConfig is type checking in TypeScript projects:
import { defineConfig } from "eslint/config" ;
export default defineConfig ({
languageOptions: {
ecmaVersion: 2022 ,
sourceType: "module"
} ,
rules: {
"no-unused-vars" : "error"
}
}) ;
Array of Configs
const { defineConfig } = require ( "eslint/config" );
module . exports = defineConfig ([
{
files: [ "**/*.js" ],
rules: {
"no-console" : "error"
}
},
{
files: [ "**/*.test.js" ],
rules: {
"no-console" : "off"
}
}
]);
Function Signature
function defineConfig ( config )
config
Config | Config[]
required
A single configuration object or an array of configuration objects. Glob patterns for files this config applies to.
Glob patterns for files to ignore.
Language-specific options. ECMAScript version to support.
sourceType
'script' | 'module' | 'commonjs'
The type of JavaScript source code.
Linting options. Disable inline configuration comments.
reportUnusedDisableDirectives
boolean | 'off' | 'warn' | 'error'
Report unused eslint-disable directives.
Shared settings for rules.
Returns the same config(s) passed in, with full type information.
Examples
Simple Config
Multiple Configs
With Plugins
TypeScript
const { defineConfig } = require ( "eslint/config" );
module . exports = defineConfig ({
languageOptions: {
ecmaVersion: 2024 ,
sourceType: "module" ,
globals: {
window: "readonly" ,
document: "readonly"
}
},
rules: {
"no-console" : "warn" ,
"no-unused-vars" : "error" ,
"prefer-const" : "error"
}
});
const { defineConfig } = require ( "eslint/config" );
const eslintPluginReact = require ( "eslint-plugin-react" );
module . exports = defineConfig ([
{
// Base config for all files
languageOptions: {
ecmaVersion: 2024 ,
sourceType: "module"
},
rules: {
"no-console" : "error"
}
},
{
// Config specific to React files
files: [ "src/**/*.jsx" ],
plugins: {
react: eslintPluginReact
},
rules: {
"react/prop-types" : "error" ,
"react/react-in-jsx-scope" : "off"
}
},
{
// Config for test files
files: [ "**/*.test.js" , "**/*.spec.js" ],
rules: {
"no-console" : "off"
}
}
]);
const { defineConfig } = require ( "eslint/config" );
const tsPlugin = require ( "@typescript-eslint/eslint-plugin" );
const tsParser = require ( "@typescript-eslint/parser" );
module . exports = defineConfig ([
{
files: [ "**/*.ts" , "**/*.tsx" ],
languageOptions: {
parser: tsParser ,
parserOptions: {
project: "./tsconfig.json"
}
},
plugins: {
"@typescript-eslint" : tsPlugin
},
rules: {
"@typescript-eslint/no-explicit-any" : "error" ,
"@typescript-eslint/no-unused-vars" : "error"
}
}
]);
import { defineConfig } from "eslint/config" ;
import tseslint from "typescript-eslint" ;
export default defineConfig ([
{
files: [ "**/*.ts" ] ,
languageOptions: {
parser: tseslint . parser ,
parserOptions: {
project: true
}
} ,
plugins: {
"@typescript-eslint" : tseslint . plugin
} ,
rules: {
"@typescript-eslint/no-explicit-any" : "error" ,
"@typescript-eslint/explicit-function-return-type" : "warn"
}
}
]) ;
Benefits
Type Safety
When using TypeScript or JSDoc, defineConfig provides full type checking:
// This will show a type error
module . exports = defineConfig ({
rules: {
"invalid-rule-name" : "error" // IDE will warn about unknown rule
}
});
IDE Autocompletion
defineConfig enables autocompletion in editors that support TypeScript:
Property name suggestions
Valid values for options
Inline documentation
Runtime Validation
While the primary purpose is type safety, defineConfig also provides runtime validation in development:
const { defineConfig } = require ( "eslint/config" );
// Runtime error if config structure is invalid
module . exports = defineConfig ({
rules: null // Error: rules must be an object
});