Skip to main content

No Shared Type Import

Enforce type layering by preventing code files from importing shared type files directly. Shared types should only be imported by .d.ts files.

Summary

The no-shared-type-import rule reports imports from shared type files (e.g., shared.d.ts) when the importing file is not a .d.ts file. This enforces a layered type architecture where shared types flow through domain .d.ts files before reaching code files.

Why Use This Rule?

  1. Enforces a clean type hierarchy: shared.d.ts to domain .d.ts to .ts code.
  2. Prevents code files from depending on low-level shared types directly.
  3. Makes refactoring safer because shared type changes only propagate through domain type files.

Examples

// In a .d.ts file — importing shared types is allowed.
// types/cli/runner.d.ts
import type { EntryCategory } from '@/types/shared.d.ts';
export type RunnerRecordSelectedCategory = EntryCategory;

// In a .ts code file — import from the domain .d.ts file.
// cli/runner.ts
import type { RunnerRecordSelectedCategory } from '@/types/cli/runner.d.ts';

Configuration

Enable the rule in your ESLint flat config:

import { noSharedTypeImport } from '@cbnventures/nova/rules/eslint';

export default [
{
plugins: {
'@cbnventures/nova': {
rules: {
'no-shared-type-import': noSharedTypeImport,
},
},
},
rules: {
'@cbnventures/nova/no-shared-type-import': ['error', {
sharedFiles: ['shared.d.ts'],
}],
},
},
];

Options

OptionTypeDefaultDescription
sharedFilesstring[][]File paths or suffixes that identify shared type files (e.g., ['shared.d.ts']).

Each entry in sharedFiles is matched against the end of the import source path. For example, 'shared.d.ts' matches any import whose source ends with /shared.d.ts.

Troubleshooting

  • Error fires on a .d.ts file. — The rule skips .d.ts files entirely. If the file is a .d.ts but the error still fires, check that the filename extension is correct.
  • Want to allow multiple shared files. — Add each shared file path to the sharedFiles array (e.g., ['shared.d.ts', 'common.d.ts']).