Lesson 8 of 8
Practice & Quiz - Production Architecture
Test your knowledge of advanced TypeScript patterns, feature-based architecture, monorepos, and accessibility with a practical exercise and 5 quiz questions.
Practical Exercise - Type-Safe Feature Module with Barrel Exports
// ─── features/products/types.ts ─────────────────────────────
export interface Product {
id: string;
name: string;
price: number;
category: 'electronics' | 'clothing' | 'books';
inStock: boolean;
}
// Discriminated union - exhaustive type-safe pattern matching
export type ProductAction =
| { type: 'ADD_TO_CART'; payload: Product }
| { type: 'REMOVE_FROM_CART'; payload: { id: string } }
| { type: 'UPDATE_QUANTITY'; payload: { id: string; qty: number } };
// ─── features/products/api.ts ─────────────────────────────────
const BASE = '/api/products';
export const productsApi = {
getAll: (): Promise<Product[]> =>
fetch(BASE).then(r => r.json()),
getById: (id: string): Promise<Product> =>
fetch(`${BASE}/${id}`).then(r => {
if (!r.ok) throw new Error(`Product ${id} not found`);
return r.json();
}),
search: (query: string, category?: Product['category']): Promise<Product[]> => {
const params = new URLSearchParams({ q: query });
if (category) params.set('category', category);
return fetch(`${BASE}?${params}`).then(r => r.json());
},
};
// ─── features/products/hooks/useProducts.ts ───────────────────
import { useQuery } from '@tanstack/react-query';
export function useProducts(category?: Product['category']) {
return useQuery({
queryKey: ['products', category],
queryFn: () =>
category
? productsApi.search('', category)
: productsApi.getAll(),
staleTime: 60_000,
});
}
// ─── features/products/index.ts - barrel export ───────────────
// Public API of the products feature - other features import from here only
export type { Product, ProductAction } from './types';
export { productsApi } from './api';
export { useProducts } from './hooks/useProducts';
export { ProductCard } from './components/ProductCard';
export { ProductList } from './components/ProductList';
// ─── Usage from another feature ───────────────────────────────
// ✅ Clean import from the barrel
import { useProducts, ProductList } from '@features/products';
// ❌ Never import deep paths from another feature
// import { ProductCard } from '@features/products/components/ProductCard';Module Quiz
What is a discriminated union and why is it useful for action types?
Tip
Tip
Practice Practice Quiz Production Architecture in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
Modern React: Use Hooks instead of class lifecycle methods
Practice Task
Note
Practice Task - (1) Write a working example of Practice Quiz Production Architecture from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Common Mistake
Warning
A common mistake with Practice Quiz Production Architecture is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready react code.
Finished this lesson?