Unknown requires narrowing before use; any bypasses checking. Network data remains untrusted regardless of its declared interface.
Before you start
You should be comfortable with JavaScript values, functions and objects. Use a TypeScript project with strict checking enabled when trying the examples. Separate what the compiler proves from what must still be checked when values arrive at runtime.
The practical goal is to reason through this situation: Parse JSON as unknown, then verify object shape and required fields. Read the walkthrough first, then try the interview exercise before opening its answer. The important part is explaining the decision and its consequences, rather than remembering a definition alone.
Step-by-step walkthrough
Step 1: Reject unsupported values
Receive the parsed response as unknown. Check for null and an object before accessing properties; JavaScript reports null as an object.
Step 2: Validate the fields
Require a string ID and name. Construct a new User from validated fields so unchecked extra properties do not silently become trusted data.
Step 3: Make failure explicit
Return an error result or throw a controlled validation error. Callers must choose how to display invalid responses instead of dereferencing them.
Worked scenario
Parse JSON as unknown, then verify object shape and required fields.
type User = { id: string; name: string };
function parseUser(value: unknown): User {
if (typeof value !== 'object' || value === null)
throw new Error('Expected a user object');
if (!('id' in value) || typeof value.id !== 'string' ||
!('name' in value) || typeof value.name !== 'string')
throw new Error('Invalid user fields');
return { id: value.id, name: value.name };
}An assertion such as value as User would skip every runtime check above. The return statement establishes a small trusted shape rather than passing the entire response onward.
Common mistake
A type assertion performs no runtime validation.
Verify the behavior
Try null, an array, a numeric ID, a missing name and a valid record. Decide whether empty strings are permitted; string checking alone does not enforce a domain rule.
Interview exercise
Accept a user response safely.
Answer and reasoning
Validate fields before constructing a User; preserve an explicit invalid-response path.
Continue learning
Compare the scenario with the TypeScript interview questions and test your understanding with the TypeScript MCQs. For terminology and implementation details, consult the reference material.