When should you use unknown instead of any?
Use unknown when a value exists but its shape has not been established. You must narrow it before accessing properties. Any permits operations without those checks and can allow an incorrect assumption to spread. Neither keyword validates incoming JSON by itself.
For a payment response, first establish that the payload is an object, then validate the fields the application requires. An assertion that labels the payload as Payment only changes what the compiler accepts. Explain what happens if the server sends a string where your application expects an amount.
How does a discriminated union improve state modeling?
Give each alternative a distinct literal field, such as status: loading, success, or error. Associate the result with success and the error message with error. Narrowing on status lets the code access the appropriate fields. This prevents a loose collection of optional properties from admitting contradictory combinations.
In our dashboard example, success requires an account object. Loading cannot accidentally display an old account as a newly completed result. Discuss whether stale cached data deserves its own explicit state rather than overloading success with hidden assumptions.
What problem should a generic solve?
A generic preserves a relationship between types. A function that returns the first element of an array should retain the element type, while accounting for an empty array. Replacing every type with a generic parameter does not automatically improve an API.
Explain the caller's benefit: passing an array of users yields a user or an absent result, rather than an unrelated unknown value. Then describe the runtime empty-array behavior. The interviewer should be able to understand both the type relationship and the execution contract.
How would you investigate a type assertion hiding a bug?
Trace where untrusted data enters and where the assertion bypasses a check. Reproduce with a small payload that violates the expected shape. Move validation to that boundary and define a useful failure result. Avoid scattering assertions through components to silence errors caused by an unclear contract.
For a practical follow-up, make the amount optional upstream. Decide whether absence is a valid business state, a defaultable value, or an error. Encode that decision and test it. A compiler error can reveal a missing product decision rather than merely inconvenient syntax.
Practice exercise
Model a request with loading, success, and error states. Write one valid example of each, then try constructing success without its result. Explain which mistakes the compiler should catch and which require runtime validation.
Next, receive an unknown JSON payload and describe the checks needed before creating the success state. Include null, an empty object, a wrong field type, and an extra harmless field. Keep the exercise small enough to explain in five minutes.
Rehearse this answer with Cluegent
Paste your proposed state model into Cluegent during preparation and ask: 'Which impossible states does this still permit, and which inputs require runtime validation?' Explain the revised model aloud.
Try Cluegent for your practice session, then repeat the explanation without suggestions. Judge your answer by its accuracy, evidence, and response to follow-up questions.
Sources checked
These official references support the guide. Product details and technical documentation can change; check the linked source for current information.
Where Cluegent helps
Cluegent supports permitted live workflows with transcript context, typed prompts, screenshot-aware answers, resume context, custom response behavior, quick action buttons, and a private desktop overlay. It is most useful when you already understand the subject and need help staying structured under pressure.
Frequently asked questions
How should I practise this topic?
Model a request with loading, success, and error states. Write one valid example of each, then try constructing success without its result. Explain which mistakes the compiler should catch and which require runtime validation.
How can Cluegent help with preparation?
Paste your proposed state model into Cluegent during preparation and ask: 'Which impossible states does this still permit, and which inputs require runtime validation?' Explain the revised model aloud.