asserts defined
- TypeScript course
- Advanced
- Lesson 42/57
- 4 min
Learn null checking with asserts x is defined.
asserts defined
asserts x is defined is a special assertion form; it narrows to all types except null and undefined. It's used to guarantee a value exists; especially useful when working with optional or nullable fields.
function assertDefined(x: unknown): asserts x is defined {
if (x === null || x === undefined) {
throw new Error("value is null or undefined");
}
}
const maybe: string | null = "hello";
assertDefined(maybe);
maybe.toUpperCase(); // string, null narrowed awayUse with nullable fields
asserts x is defined is used to narrow a nullable value (e.g. string | null) to string. When working with optional properties, it guarantees the value exists and the subsequent code can be used safely without null checks.
interface User {
email: string | null;
}
function getEmail(u: User): string {
assertDefined(u.email);
return u.email; // string, null narrowed away
}Practice Handle a nullable email with assertDefined
Define an assertDefined(x: unknown): asserts x is defined function (with throw new Error("null or undefined") when x === null || x === undefined). Define a User interface with email: string | null. Write a getEmail(u: User): string function: call assertDefined(u.email), then return u.email. Print the result via console.log(getEmail({ email: "a@b.com" })).
Starter code
// assertDefined fonksiyonunu tanımla (asserts x is defined, throw ile)
// User interface tanımla (email: string | null)
// getEmail(u: User): string fonksiyonunu assertDefined ile yaz
// getEmail({ email: "a@b.com" }) sonucunu yazdırSample solution
function assertDefined(x: unknown): asserts x is defined {
if (x === null || x === undefined) {
throw new Error("null or undefined");
}
}
interface User {
email: string | null;
}
function getEmail(u: User): string {
assertDefined(u.email);
return u.email;
}
console.log(getEmail({ email: "a@b.com" }));Expected output
a@b.com
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 What does asserts x is defined do?
- It makes the value undefined
- It makes the type string
- It produces a value at runtime
- It narrows to all types except null and undefined Correct answer
Explanation: asserts x is defined narrows to all types except null and undefined; it reduces a nullable value to its non-null type and makes subsequent code safe.
2 asserts x is defined
function assertDefined(x: unknown): ____ {
if (x === null || x === undefined) throw new Error("null or undefined");
}
function assertDefined(x: unknown): asserts x is defined {
if (x === null || x === undefined) throw new Error("null or undefined");
}
Explanation: The asserts x is defined return type tells TypeScript that x is neither null nor undefined when the function completes.
3 asserts defined parts
| Term | Meaning |
|---|---|
asserts x is defined |
narrows out null and undefined |
nullable değer |
reduced to string via defined |
x === null || x === undefined |
body check |
opsiyonel özellik |
safe access via defined |