Deserialization
Start from unknown
Data from JSON, files, or the network arrives without a compile time type. Parse it into unknown first, which forces every reader to prove the shape before use.
JSON.parse returns any, so assign the result to an unknown binding right away. From that point the checker blocks property access until a guard passes. The program parses one small object and prints it.
const data: unknown = JSON.parse("{\"ok\":true}"); console.log(data);
{ ok: true }
Guard every field
A guard is a small function that answers one question, such as whether a value is a string. Combine tiny guards to describe bigger shapes without writing one giant check.
Return plain booleans at first and upgrade to type predicates later. Even boolean guards document the expected shape and centralize every runtime check. The program asks the guard about a word and prints the answer.
function isString(value: unknown): boolean { return typeof value === "string"; } const flag: boolean = isString("hi"); console.log(flag);
true
Nested objects
Objects need checks for their container shape before any field check. Reject null and non objects early, then test for the presence of each required key.
Write the guard as a type predicate so a passing check narrows the value for the rest of the scope. Later code then reads fields with full editor support. The program asks the guard about a user, an empty object and null.
type User = { name: string; }; function isUser(value: unknown): value is User { if (typeof value !== "object") { return false; } if (value === null) { return false; } return "name" in value; } console.log(isUser({ name: "Ada" }), isUser({}), isUser(null));
true false false
Arrays of records
Arrays add one more layer: prove the value is an array, then guard each element. Collect the elements that pass and skip or report the rest.
Loop with a plain for of, push matching items into a typed result array, and return it. Callers receive clean records and never touch raw unknown again. The program filters a mixed list and prints how many users survived.
type User = { name: string; }; function isUser(value: unknown): value is User { if (typeof value !== "object") { return false; } if (value === null) { return false; } return "name" in value; } function allUsers(values: Array<unknown>): Array<User> { const found: Array<User> = []; for (const item of values) { if (isUser(item)) { found.push(item); } } return found; } const mixed: Array<unknown> = [{ name: "Ada" }, 5, null, { name: "Bob" }]; console.log(allUsers(mixed).length);
2
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
Why assign the result of
JSON.parseto anunknownbinding?Answer
It forces every reader to prove the shape before use
JSON.parsegivesany, which switches checking off;unknownkeeps the value safe until a guard passes. -
What does a type predicate add to a boolean guard?
Answer
A true result narrows the value for the rest of the scope
The
value is Userreturn type is what lets later code read the fields. -
Which check comes first for an object guard?
Answer
That the value is an object and not null
typeof value === "object"also matches null, so the null check follows before any field test. -
What does a guard for an array check first?
Answer
That the value is an array, then each element
The container shape comes first, and then every element is guarded on its own.
-
What does this program print?
const data: unknown = JSON.parse("{\"ok\":true}"); console.log(data);Answer
{ ok: true }
The parse builds the object, and printing it needs no property access, so the unknown binding is enough.
-
What does this program print?
function isString(value: unknown): boolean { return typeof value === "string"; } console.log(isString("hi"), isString(5), isString(null));Answer
true false false
Only the word is a string, so only the first call gives true.
-
What does this program print?
type User = { name: string }; function isUser(value: unknown): value is User { return ( typeof value === "object" && value !== null && "name" in value ); } console.log(isUser({ name: "Ada" }), isUser({}), isUser(null));Answer
true false false
The first object has the name key, the empty one does not, and null fails the object check.
-
What does this program print?
type User = { name: string }; function isUser(value: unknown): value is User { return typeof value === "object" && value !== null && "name" in value; } const values: Array<unknown> = [{ name: "Ada" }, 5, { name: "Bob" }]; let count = 0; for (const item of values) { if (isUser(item)) { count++; } } console.log(count);Answer
2
The number is skipped, and the two objects with a name pass the guard.
-
What does the compiler say about this program?
const data: unknown = JSON.parse("{\"ok\":true}"); console.log(data.ok);Answer
error TS18046: 'data' is of type 'unknown'.
An unknown value allows no property access until a guard proves the shape.
-
What does the compiler say about this program?
type User = { name: string }; function isUser(value: unknown): value is User { return typeof value === "object" && value !== null && "name" in value; } const item: unknown = { name: "Ada" }; if (isUser(item)) { console.log(item.name.toFixed(2)); }Answer
error TS2339: Property 'toFixed' does not exist on type 'string'.
The guard narrows the value to a user, whose name is a string, so a number method is refused.
Article author: Arthur Isaev