Código que envuelve una función, biblioteca o componente para agregar funcionalidades.
O simplificar su uso. Actúa como intermediario sin modificar el código original.
// Wrapper para fetch con manejo de errores async function fetchWrapper<T>(url: string, options?: RequestInit): Promise<T> { try { const response = await fetch(url, { ...options, headers: { 'Content-Type': 'application/json', ...options?.headers } }); if (!response.ok) { throw new Error(`HTTP ${response.status}`); } return await response.json(); } catch (error) { console.error('Fetch error:', error); throw error; } } // Uso simplificado const usuarios = await fetchWrapper<Usuario[]>('/api/usuarios'); // Wrapper de componente React function withAuth<P extends object>(Component: React.ComponentType<P>) { return function AuthWrapper(props: P) { const { user, isLoading } = useAuth(); if (isLoading) return <Loading />; if (!user) return <Redirect to="/login" />; return <Component {...props} user={user} />; }; } const ProtectedDashboard = withAuth(Dashboard); // Wrapper para localStorage con JSON const storage = { get: <T>(key: string): T | null => { const item = localStorage.getItem(key); return item ? JSON.parse(item) : null; }, set: <T>(key: string, value: T): void => { localStorage.setItem(key, JSON.stringify(value)); } };