Organize TypeScript modules: explicit public entry points, type-only imports and exports, path aliases that match the runtime, and no circular dependencies.
.cursor/rules/typescript-module-organization.mdc. Downloaded files need to be placed in that repository path.---
description: "Organize TypeScript modules: explicit public entry points, type-only imports and exports, path aliases that match the runtime, and no circular dependencies."
globs: "**/*"
alwaysApply: false
---
Apply these rules when organizing TypeScript modules and imports.
## Public API
- Give each package or feature one public entry point (`index.ts` or the `exports` field in `package.json`) and import other features only through it. Keep internal files unexported from that entry.
- Use barrel files only at those public boundaries. Avoid barrels that re-export a whole folder inside an application: they make bundlers and test runners load more modules than needed and easily create import cycles.
## Type-only imports and exports
- Use `import type` / `export type` (or inline `type` specifiers) for types. With `verbatimModuleSyntax` enabled, TypeScript requires this and keeps runtime imports exactly as written.
```ts
// features/users/index.ts — public API of the users feature
export { createUser, getUser } from './service';
export type { User, CreateUserInput } from './types';
```
## Where types live
- Keep a type next to the code that owns it. Move types to a shared `types.ts` only when several modules use them; avoid one global `types/` folder that everything imports.
## Path aliases
- Use a small number of aliases (for example `@/*` → `src/*`). `paths` in tsconfig only affects type-checking: the bundler, test runner and Node runtime must resolve the same alias (framework config, Vite `resolve.alias`, `package.json` `imports` with `#` specifiers, or `tsconfig-paths`).
## Namespace imports
- Use `import * as x from '...'` for large utility modules or SDKs where the prefix aids reading (`import * as z from 'zod'` is not needed; `import * as path from 'node:path'` is fine). Otherwise prefer named imports.
## Circular dependencies
- Keep dependencies pointing one way (UI → features → lib). Detect cycles in CI with a tool such as `madge --circular` or the `import/no-cycle` ESLint rule, and break them by moving shared code down a layer.
## Documentation
- Document each public entry point's exports with TSDoc comments; internal modules need comments only where intent is unclear.
Cursor rules for Next.js 14+ App Router projects updated for Next.js 16 with async params, validated Route Handlers, generateMetadata and proxy.ts in place of middleware.
Cursor rules for strict TypeScript: compiler checks beyond strict, interfaces vs type aliases, real type guards, discriminated unions, utility types and branded IDs.
Cursor rules for Tailwind CSS v4: automatic class sorting, mobile-first breakpoints, CVA variants with merging, class-based dark mode via @custom-variant, and CSS-first @theme configuration.
Cursor
architecture
Illustrative input and expected result. This example is a verification guide, not a recorded model run.
Content reviewed .
A React + Vite app has an index.ts barrel in every folder, an import cycle between features/users and features/teams, and '@/...' imports that work in VS Code but fail in Vitest.
Using the TypeScript Module Organization rule, propose a module structure and the config changes needed. tsconfig has verbatimModuleSyntax: true.
Barrels kept only for feature public APIs, internal imports switched to direct paths, type-only imports marked with `import type`, the users/teams cycle broken by moving the shared type into lib, Vite and Vitest resolve.alias matching tsconfig paths, and a madge --circular CI step.
Cursor applies project rules stored as .mdc files in .cursor/rules. Each rule's frontmatter (description, globs, alwaysApply) decides whether it is always included, attached to matching files, chosen by the agent from its description, or only used when you @-mention it. Cursor also reads AGENTS.md in the project root and subdirectories for plain-Markdown instructions.
.cursor/rules/typescript-module-organization.mdcStarting point, not a tested configuration for your repository. Attached when files matching the globs are in context (alwaysApply: false with globs). Review the instructions, adapt the file paths and run your project's existing checks.
---
description: "TypeScript Module Organization project conventions"
globs: "**/*"
alwaysApply: false
---
Apply these rules when organizing TypeScript modules and imports.
## Public API
- Give each package or feature one public entry point (`index.ts` or the `exports` field in `package.json`) and import other features only through it. Keep internal files unexported from that entry.
- Use barrel files only at those public boundaries. Avoid barrels that re-export a whole folder inside an application: they make bundlers and test runners load more modules than needed and easily create import cycles.
## Type-only imports and exports
- Use `import type` / `export type` (or inline `type` specifiers) for types. With `verbatimModuleSyntax` enabled, TypeScript requires this and keeps runtime imports exactly as written.
```ts
// features/users/index.ts — public API of the users feature
export { createUser, getUser } from './service';
export type { User, CreateUserInput } from './types';
```
## Where types live
- Keep a type next to the code that owns it. Move types to a shared `types.ts` only when several modules use them; avoid one global `types/` folder that everything imports.
## Path aliases
- Use a small number of aliases (for example `@/*` → `src/*`). `paths` in tsconfig only affects type-checking: the bundler, test runner and Node runtime must resolve the same alias (framework config, Vite `resolve.alias`, `package.json` `imports` with `#` specifiers, or `tsconfig-paths`).
## Namespace imports
- Use `import * as x from '...'` for large utility modules or SDKs where the prefix aids reading (`import * as z from 'zod'` is not needed; `import * as path from 'node:path'` is fine). Otherwise prefer named imports.
## Circular dependencies
- Keep dependencies pointing one way (UI → features → lib). Detect cycles in CI with a tool such as `madge --circular` or the `import/no-cycle` ESLint rule, and break them by moving shared code down a layer.
## Documentation
- Document each public entry point's exports with TSDoc comments; internal modules need comments only where intent is unclear.
Ask for a small change to one matching file. Check the tool's loaded context or instruction view, inspect the diff for the requested convention, and run the relevant build or test. A well-formatted file does not guarantee the model follows every instruction.
Cursor's docs recommend keeping individual rules under 500 lines and splitting large rules into composable ones. Cursor's documentation states that it supports AGENTS.md in the project root and in subdirectories. Use AGENTS.md for plain-Markdown instructions you also want other tools to read, and .mdc rules when you need Cursor's scoping options (globs, description-based selection, always-apply).
Documentation checked 2026-11-05: Cursor docs: Rules
Cursor rules for Next.js 14+ App Router projects updated for Next.js 16 with async params, validated Route Handlers, generateMetadata and proxy.ts in place of middleware.
Cursor rules for strict TypeScript: compiler checks beyond strict, interfaces vs type aliases, real type guards, discriminated unions, utility types and branded IDs.
Cursor rules for Tailwind CSS v4: automatic class sorting, mobile-first breakpoints, CVA variants with merging, class-based dark mode via @custom-variant, and CSS-first @theme configuration.