Complete worked example
typescript
import {createContext,useContext} from "react";
type Saved={ids:readonly string[];toggle:(id:string)=>void};
const SavedContext=createContext<Saved|null>(null);
export function useSaved():Saved{const value=useContext(SavedContext);if(value===null)throw new Error("Saved provider required");return value;}- Reasoning
- The hook establishes the provider invariant before exposing a nonnullable shared capability.