QuizNot attempted
Typed context with a guard hook
1–4 to choose, Enter to submit
Show answer & explanation
Correct answer:
UserCtx; it throws a clear error at render time
useContextreturnsUserCtx | undefined, and thethrownarrows it, so the hook's inferred return type is plainUserCtxand consumers never writevalue?.user- a missing provider becomes a loud error instead of a silent
undefined. - Not UserCtx | undefined: the
if (!ctx) throwremovesundefinedby control-flow narrowing. - Not 'returns an empty object': that is the
createContext({} as UserCtx)hack, which lies to the compiler and crashes later onsetUser. - Not any:
createContext<T>keepsT.