ESLint Plugin for TypeORM-TypeScript Type Consistency
eslint-plugin-typeorm-typescript is an ESLint plugin designed to ensure type consistency between TypeORM entity definitions and their corresponding TypeScript types. It identifies discrepancies in primitive types (e.g., `varchar` vs. `number`), handles driver-specific type parsing (notably `bigint` and `decimal` which are often strings), and enforces correct nullability for columns and relations. The current stable version is `0.5.3`. The project appears to have an active release cadence, frequently introducing new rules and updating support for newer ESLint and TypeScript-ESLint versions. A key differentiator is its ability to catch subtle TypeORM-specific type mismatches that static TypeScript analysis alone cannot, particularly around default nullability behaviors and specific database type mappings. It supports both legacy and modern ESLint flat configurations, providing flexibility for different project setups.
Common errors
-
Plugin 'typeorm-typescript' was referenced but not found.
cause ESLint could not load the plugin, typically due to incorrect installation or an improper path in the configuration.fixEnsure `eslint-plugin-typeorm-typescript` is installed as a dev dependency (`npm install -D eslint-plugin-typeorm-typescript`) and correctly referenced in your `.eslintrc.json` (`"plugins": ["typeorm-typescript"]`) or `eslint.config.mjs` (`typeormTypescriptPlugin` or `typeormTypescriptRecommended`). -
Parsing error: 'import' and 'export' may only appear at the top level
cause This error often occurs when attempting to use ES Module `import` syntax in a file configured as CommonJS, or in an older Node.js environment without full ESM support.fixIf using an ESLint flat config file (e.g., `eslint.config.mjs`), ensure your environment (e.g., Node.js 20+) supports ESM. If sticking with CommonJS for legacy `.eslintrc.js` files, use `require()` statements instead of `import`. -
Configuration for rule 'typeorm-typescript/enforce-column-types' is invalid: Value must be an object.
cause An array was provided for rule options where an object was expected, or the object structure for rule options is incorrect.fixWhen providing options for a rule, ensure they are passed as the second element of an array where the first element is the rule's severity level (e.g., `"error"`). The options themselves should be a valid JavaScript object: `["error", { "driver": "sqlite" }]`.
Warnings
- breaking Version `0.5.3` drops official support for Node.js 18.
- breaking The parsing logic for `bigint` and `decimal` column types changed in `v0.5.0` to correctly align with database driver behavior.
- breaking Version `0.4.0` introduced support for ESLint v9 and flat configuration, along with dual ES Module and CommonJS support. This requires updating your ESLint configuration file.
- gotcha TypeORM relations are nullable by default, unlike regular columns, which can lead to unexpected type inconsistencies if not explicitly handled.
Install
-
npm install eslint-plugin-typeorm-typescript -
yarn add eslint-plugin-typeorm-typescript -
pnpm add eslint-plugin-typeorm-typescript
Imports
- typeormTypescriptRecommended
const typeormTypescriptRecommended = require('eslint-plugin-typeorm-typescript/recommended');import typeormTypescriptRecommended from 'eslint-plugin-typeorm-typescript/recommended';
- typeormTypescriptPlugin
const typeormTypescriptPlugin = require('eslint-plugin-typeorm-typescript');import typeormTypescriptPlugin from 'eslint-plugin-typeorm-typescript';
- Rules Object (Legacy)
import typeormTypescript from 'eslint-plugin-typeorm-typescript';
{ "plugins": ["typeorm-typescript"], "rules": { "typeorm-typescript/enforce-column-types": "error" } }
Quickstart
import eslint from '@eslint/js';
import tseslint from 'typescript-eslint';
import typeormTypescriptRecommended from 'eslint-plugin-typeorm-typescript/recommended';
export default tseslint.config(
eslint.configs.recommended,
...tseslint.configs.recommended,
typeormTypescriptRecommended,
{
files: ['**/*.ts', '**/*.tsx'],
languageOptions: {
parserOptions: {
project: './tsconfig.json'
}
},
rules: {
"typeorm-typescript/enforce-column-types": ["error", { "driver": "postgres" }],
"typeorm-typescript/enforce-relation-types": "warn",
"typeorm-typescript/enforce-consistent-nullability": ["error", { "specifyNullable": "always" }]
}
}
);