TypeScript Support
Eslint Plugin Boundaries provides first-class TypeScript support, including full type definitions for configuration and seamless integration with TypeScript projects.
Prerequisites
To use this plugin in a TypeScript project, you'll need to install the following dependencies:
npm install --save-dev @typescript-eslint/eslint-plugin @typescript-eslint/parser eslint-import-resolver-typescript
These packages provide:
- @typescript-eslint/parser: Parses TypeScript code for ESLint
- @typescript-eslint/eslint-plugin: Core TypeScript ESLint rules
- eslint-import-resolver-typescript: Resolves TypeScript imports and path mappings
Configuration
Configure your eslint.config.js file to use the TypeScript parser and resolver:
import boundaries from "eslint-plugin-boundaries";
import typescriptParser from "@typescript-eslint/parser";
import typescriptEslintPlugin from "@typescript-eslint/eslint-plugin";
export default [{
languageOptions: {
parser: typescriptParser,
},
plugins: {
"@typescript-eslint": typescriptEslintPlugin,
boundaries,
},
settings: {
"import/resolver": {
typescript: {
alwaysTryTypes: true,
},
},
},
}];
Path Mapping Support
The eslint-import-resolver-typescript automatically detects custom path mappings defined in your tsconfig.json file, making it fully compatible with TypeScript path aliases:
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@modules/*": ["src/modules/*"],
"@shared/*": ["src/shared/*"]
}
}
}
Type Definitions
The plugin exports comprehensive TypeScript type definitions to provide autocomplete and type safety for your configuration.
Main Config Type
The primary export is the Config type, which represents a fully typed Flat Config:
import type { Config } from "eslint-plugin-boundaries";
const config: Config = {
plugins: {
boundaries,
},
settings: {
"boundaries/elements": [],
"boundaries/ignore": ["**/ignored/**/*.js"],
},
rules: {
"boundaries/element-types": [
"error",
{ default: "disallow", rules: [] },
],
},
};
Custom Plugin Names
The type system supports renaming the plugin when loading it in the plugins property:
import type { Config } from "eslint-plugin-boundaries";
const config: Config<"custom-boundaries"> = {
plugins: {
"custom-boundaries": boundaries, // Renamed prefix
},
settings: {
"boundaries/elements": [], // Always uses original prefix in settings
"boundaries/ignore": ["**/ignored/**/*.js"],
},
rules: {
"custom-boundaries/element-types": 2, // Must use renamed prefix
},
};
Settings always use the boundaries/ prefix regardless of the plugin name, as ESLint doesn't namespace settings by plugin name.
Granular Type Exports
In addition to the main Config type, the plugin exports individual subtypes for fine-grained type safety:
Settings- Plugin settings configurationRules- Rule configurationElementDescriptor- Element type definitionsElementTypesRule- Element types rule configurationElementTypesRuleOptions- Rule optionsElementSelector- Element selector syntaxIgnoreSetting- Ignore pattern configuration- ...
This modular approach allows you to import only what you need while maintaining full autocomplete and type checking:
import type {
Config,
Settings,
Rules,
ElementDescriptor,
ElementTypesRuleOptions,
} from "eslint-plugin-boundaries";
const elementDescriptor: ElementDescriptor = {
type: "module",
pattern: "src/modules/*",
capture: ["module"],
};
const settings: Settings = {
"boundaries/elements": [elementDescriptor],
};
const rules: Rules = {
"boundaries/element-types": ["error", { default: "disallow", rules: [] }],
};
const config: Config = {
files: ["**/*.js", "**/*.ts"],
settings,
rules,
};
Benefits of TypeScript Integration
Using the plugin with TypeScript provides several advantages:
- Type Safety: Catch configuration errors at development time
- Autocomplete: Get intelligent suggestions in your IDE
- Documentation: Inline type information serves as documentation
- Refactoring: Safely rename and restructure configurations
- Path Mapping: Automatic support for TypeScript path aliases
Example Repository
For a complete working example of TypeScript integration, see the example TypeScript repository, which includes a fully tested configuration.
Troubleshooting
If you encounter issues with TypeScript support:
- Ensure all dependencies are installed with compatible versions
- Verify that
eslint-import-resolver-typescriptis configured in settings - Check that your
tsconfig.jsonis valid and accessible - Confirm that the TypeScript parser is correctly specified in
languageOptions - Refer to the example repository for a known working configuration