Model wallet connection as a discriminated union
React + TypeScript Interview Core · TypeScript State Modeling
Represent disconnected, connecting, connected, wrong network, and rejected states with exhaustive rendering.
Prompt
Model wallet connection as a discriminated union
This is a hands-on rep. Attempt the drill before reading the model answer, then narrate the tradeoffs as if an interviewer is watching.
Avoid nullable address/provider fields sprinkled through components.
What to ground before answering
Represent disconnected, connecting, connected, wrong network, and rejected states with exhaustive rendering.
Focus vocabulary: TypeScript, wallet, union types.
The useful mental model is not to memorize a perfect answer. It is to explain what owns the data, what can fail, what the user sees, and what test would prove the behavior.
Render wallet connection states exhaustively
Make the behavior executable before comparing against the model answer.
type WalletState =
| { tag: 'disconnected' }
| { tag: 'connecting' }
| { tag: 'connected'; address: string; chainId: number }
| { tag: 'wrong-network'; address: string; expectedChainId: number; actualChainId: number }
| { tag: 'rejected'; reason: string };
function walletCta(state: WalletState): string {
// TODO: return the primary UI call-to-action for each wallet state.
return 'Connect wallet';
}Interview explanation prompt
- What problem is this practice item really testing?
- What state or contract boundary must be explicit?
- What edge case would cause a production regression?
- What would you test first?
- How would you explain the tradeoff in two minutes?
Self-grade
- Strong answer handles the edge cases before polishing syntax.
- Strong answer explains why the chosen type or function boundary prevents bugs.
- Weak answer passes only the happy path or hides uncertainty in booleans and nullable fields.
Model Answer
function walletCta(state: WalletState): string {
switch (state.tag) {
case 'disconnected':
return 'Connect wallet';
case 'connecting':
return 'Connecting...';
case 'connected':
return 'Continue';
case 'wrong-network':
return 'Switch network';
case 'rejected':
return 'Try again';
}
}Recall before moving on
- What is the one-sentence answer for "Model wallet connection as a discriminated union"?
- Which real experience from PR TIMES, React/TypeScript migration, or systems work supports it?
- What edge case would you volunteer before the interviewer asks?
- What is the smallest test or artifact that proves the design works?