Skip to main content

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.

10 minBy Priygop Editorial TeamUpdated 2026Peer Reviewed & Verified

Practical Exercise - Type-Safe Feature Module with Barrel Exports

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

Module Quiz
Question 1 of 5

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.

Diagram
Loading diagram…

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.

Hands-on Practice Tools for this Course

Test what you learn using these free in-browser developer utilities (no signup required).

All 40+ Tools

Topics in This Module

0/8 done