Does a TypeScript interface validate an API response?
Understand the idea, explain it in your own words and use the rubric to find your next learning gap.
The idea behind the question
Type annotations help at compile time but do not inspect network bytes at runtime. Unknown external data needs explicit validation or narrowing.
A response typed as User can still contain a missing email or an unexpected null.
A practical way to learn it
Validate a successful and malformed user response without using any or a blind assertion.
Open TypeScript playgroundThe related tool opens separately; your interview answer stays here. Written practice is self-guided, without automatic code grading.
Build an explanation you understand
- State what the concept does in plain language.
- Apply it to the example and make your assumptions explicit.
- Explain a trade-off, failure case and how you would test it.
- Distinguish your actual experience from a proposed approach.
How would you explain it?
Single-question timers reset on refresh. Full mock sessions can resume from this browser.
Strengthen your explanation
Compare reasoning, not identical wording. Reference answers describe a possible approach; replace hypothetical statements with truthful details from your own work.
One way to answer
I would receive untrusted data as unknown and validate the required shape before treating it as a domain object. A schema library or explicit guards can check types, required fields and useful business constraints. I would distinguish an assertion from validation: as User changes the compiler's view but does not repair or verify the value. I would handle malformed responses deliberately and keep optional and nullable fields clear. This also gives the UI a trustworthy error path instead of crashing deep inside rendering.
What a strong explanation should cover
Tick only the points your own answer addresses. This is self-assessment, not AI scoring.
0 / 3 points self-reportedWatch out for
- Using any or a type assertion to hide an unvalidated boundary.
Now go one level deeper
- What can a discriminated union represent?
- Where would you place shared response validation?
Further reading
Further-reading links support learning; the reference is Sukoaa-authored, not an official employer answer key.