Sub topic 8
Defining a feature's public API with a barrel
This chapter assumes you know TypeScript modules, Angular standalone components, and the feature folder layout from the earlier chapters. It shows how to give each feature a small, explicit public surface.
Why a feature needs a public API
A feature folder holds components, stores, models and helpers. Other code should not depend on most of them. If customers imports orders/data-access/orders.store directly, any rename or move inside orders can break customers. The feature's internals have become part of its contract.
A public API fixes this. The feature declares the few symbols others may import. Everything else is private, so you can change it freely.
The barrel file
A barrel is an index.ts at the feature root that re-exports the public symbols.
// src/app/orders/index.ts
export { ORDERS_ROUTES } from './orders.routes';
export type { Order } from './data-access/order.model';This barrel exposes two things: the route configuration, so the app can lazy-load the feature, and the Order type, so other features can describe order data. OrdersStore, OrderListComponent and OrderDetailComponent are not listed, so they are private by convention.
TypeScript does not enforce this. Nothing stops a developer from importing a private file by its long path. The barrel is an agreement, and later chapters add lint rules that enforce it.
Choosing what to export
Use these rules to decide what goes in the barrel.
- Export the route array if the app lazy-loads the feature.
- Export types and interfaces that other features need to describe the feature's data.
- Export a component only if another feature renders it, such as a customer badge.
- Do not export stores, helpers or child components by default. Add them only when a real consumer needs them.
Keep the barrel small. A barrel that exports everything gives you no boundary, only a shorter import path.
Using export type
Use export type for interfaces and type aliases. These disappear at compile time. Marking them as type-only makes your intent clear and works with the isolatedModules compiler option, which Angular projects often enable.
Barrels and lazy loading
A barrel can undo lazy loading. Suppose the app routes file imports the barrel statically:
import { ORDERS_ROUTES } from '@app/orders';Because this is a static import, the bundler places the whole orders feature, including its routes, in the main bundle. For lazy loading, use a dynamic import of the barrel or of the route file:
{
path: 'orders',
loadChildren: () => import('@app/orders').then((m) => m.ORDERS_ROUTES),
}Other features that import only the Order type are safe. Type-only imports are erased and add nothing to the bundle.
Common mistakes
- Barrel cycles. If
orders/index.tsre-exports a file that imports from@app/orders, you get a circular import. Inside a feature, import from sibling files with relative paths, never through the feature's own barrel. - Exporting too much. Each extra export is a promise to every consumer. Remove exports you no longer need.
- Static imports of a lazy feature. One static import of a value from the barrel can pull the feature into the main bundle. Check the build output.
- Barrels in every folder. Add them at feature boundaries. Barrels at every subfolder add maintenance and cycles with little benefit.
Exercise
The customers feature needs to show an order's total, and it needs Order and OrdersStore. The orders barrel exports only ORDERS_ROUTES and the Order type. What should the developer do, and what should they avoid?
Show answer
Import the Order type from the barrel. For the store, ask whether customers really needs it. If it does, add it to the barrel as a deliberate decision, or better, have the orders feature expose a narrower API. The developer should avoid importing orders/data-access/orders.store by its deep path, because that bypasses the public API and couples customers to orders' internals.
Exercise
Why can a static import of ORDERS_ROUTES in the app's routes file defeat lazy loading?
Show answer
A static import makes the bundler include the imported module and its dependencies in the main bundle. Lazy loading needs a dynamic import() so the feature goes into its own chunk.