Sub topic 6
Public APIs and module boundaries
This chapter assumes you can read TypeScript and React, and that you already group code by feature, as in the earlier chapters. Here you will decide which parts of a feature other code may use, and which parts stay private.
What a module boundary is
A module boundary is a line around a feature. Code inside the line can use any file in the feature. Code outside the line may use only the names the feature chooses to share. That list of names is the feature's public API.
Without a boundary, other features import whatever file is convenient. Later you rename a file or split a hook, and imports break in many places. With a boundary, only the public API has to stay stable. Everything else can change freely.
The index file is the public API
In TypeScript, the simplest public API is the feature's index.ts. It re-exports only what other features need.
// src/features/cart/index.ts
export { CartList } from "./components/CartList";
export { useCart } from "./hooks/useCart";
export type { CartItem } from "./types";Other code imports from the feature as a whole:
import { CartList, useCart } from "@/features/cart";It does not reach into the internals:
// Avoid: depends on the cart's private file layout
import { CartList } from "@/features/cart/components/CartList";The @/ prefix is the path alias from the previous chapter.
Choosing what to export
Export less than you think you need. Adding an export later is easy. Removing one means finding every caller.
- Export the components and hooks that other features actually render or call.
- Export types that appear in those signatures, using
export type. - Keep helpers, internal components, and API functions private unless another feature needs them.
How dependencies should flow
Boundaries also limit which features may depend on which. A common rule is that features use shared/ code, and shared/ never imports from features. Features should avoid importing from each other when they can. When one must, it goes through the public API.
The diagram shows allowed imports.
Common errors
- Circular imports. If
cartimports fromordersandordersimports fromcart, modules can load before their exports exist, which leads toundefinedvalues at runtime. Move the shared piece intoshared/, or merge the two features. - Barrel files that export everything. An
index.tswithexport *from every file makes the whole feature public. The boundary exists in name only. - Importing the index from inside the feature. A file inside
cartthat imports from@/features/cartcan create a cycle with its own index. Use relative imports within a feature. - Relying on convention alone. Developers forget rules, and reviewers miss them. The enforcement chapter covers a lint rule that reports deep imports automatically.
Practice
Exercise
A teammate imports formatCartTotal from @/features/cart/utils/formatCartTotal. The cart's index.ts does not export it. Give two ways to fix this, and say when you would choose each.
Show answer
Option 1: export formatCartTotal from cart/index.ts and import it from @/features/cart. Choose this if other features really need it, since it becomes part of the stable public API. Option 2: move the function to shared/lib if it is generic, or have the teammate write their own helper if it is only used in one place. Choose this if the function is not specific to the cart, or if exposing it would widen the public API for a single caller.
Exercise
Why should files inside the cart feature use relative imports for each other instead of @/features/cart?
Show answer
Importing through the feature's own index can create a circular import, because the index loads the file that is importing it. Relative imports go straight to the target file and avoid the cycle.