Truthiness narrowing is when TypeScript uses checks like if or ! to refine the type of a variable based on whether it is truthy or falsy.
Click to reveal answer
beginner
Which values are considered falsy in JavaScript and TypeScript?
The falsy values are: false, 0, '' (empty string), null, undefined, and NaN. Everything else is truthy.
Click to reveal answer
beginner
How does TypeScript narrow the type of a variable in this code?<br><pre>let value: string | null = getValue();
if (value) {
// What is the type of value here?
}</pre>
Inside the if (value) block, TypeScript narrows value to string because null is falsy and filtered out.
Click to reveal answer
intermediate
What happens to the type of a variable after a if (!variable) check?
After if (!variable), TypeScript narrows the variable's type to only the falsy types it can be, excluding truthy ones.
Click to reveal answer
beginner
Why is truthiness narrowing useful in TypeScript?
It helps catch errors early by letting TypeScript know when a variable cannot be null or undefined, so you can safely use it without extra checks.
Click to reveal answer
Which of these values is truthy in TypeScript?
A"hello"
B"" (empty string)
Cnull
D0
✗ Incorrect
Only non-empty strings like "hello" are truthy. 0, empty string, and null are falsy.
Given let x: string | null;, what is the type of x inside if (x) block?
Astring | null
Bstring
Cnull
Dnever
✗ Incorrect
Inside the if block, null is excluded because it is falsy, so x is narrowed to string.
What does if (!value) check for in TypeScript?
Avalue is falsy
Bvalue is a string
Cvalue is truthy
Dvalue is a number
✗ Incorrect
The ! operator negates truthiness, so it checks if value is falsy.
Which of these is NOT a falsy value in TypeScript?
Aundefined
BNaN
C"false" (string)
D0
✗ Incorrect
The string "false" is truthy because it is a non-empty string.
Why does TypeScript use truthiness narrowing?
ATo convert all variables to boolean
BTo make variables immutable
CTo remove all null values from code
DTo help safely use variables by refining their types based on runtime checks
✗ Incorrect
Truthiness narrowing helps TypeScript know when variables are definitely not null or undefined, improving safety.
Explain how TypeScript uses truthiness narrowing to refine variable types in conditional statements.
Think about how an if statement filters out falsy values.
You got /4 concepts.
List the common falsy values in TypeScript and explain why knowing them helps with truthiness narrowing.
Recall the basic falsy values from JavaScript.
You got /3 concepts.
Practice
(1/5)
1. What does truthiness narrowing in TypeScript help you do inside an if statement?
easy
A. It forces TypeScript to ignore type errors.
B. It converts all values to strings automatically.
C. It tells TypeScript the value is not null, undefined, or falsy, so you can use it safely.
D. It makes the code run faster by skipping checks.
Solution
Step 1: Understand what truthiness means
Truthiness means a value is considered true in a boolean context, so it is not null, undefined, 0, empty string, or false.
Step 2: Apply truthiness narrowing in TypeScript
Inside an if statement, TypeScript narrows the type by excluding falsy values, letting you safely use the value without extra checks.
Final Answer:
It tells TypeScript the value is not null, undefined, or falsy, so you can use it safely. -> Option C
Quick Check:
Truthiness narrowing = safe value use [OK]
Hint: If inside if(value), value is truthy and safe to use [OK]
Common Mistakes:
Thinking it converts values to strings
Believing it skips type checks
Assuming it improves runtime speed
2. Which of the following TypeScript if statements correctly uses truthiness narrowing?
easy
A. if (value == false) { console.log(value); }
B. if (value === 0) { console.log(value); }
C. if (value = true) { console.log(value); }
D. if (value !== null && value !== undefined) { console.log(value); }
Solution
Step 1: Check each if condition for truthiness narrowing
if (value !== null && value !== undefined) { console.log(value); } explicitly checks for not null and not undefined, which is a common way to narrow truthy values.
Step 2: Identify incorrect syntax or logic
if (value == false) { console.log(value); } checks if value is false (falsy), which is opposite of truthiness narrowing. if (value = true) { console.log(value); } uses assignment (=) instead of comparison (===). if (value === 0) { console.log(value); } checks if value equals 0, which is falsy, so it does not narrow to truthy.
Final Answer:
if (value !== null && value !== undefined) { console.log(value); } -> Option D
Quick Check:
Correct syntax and logic = if (value !== null && value !== undefined) { console.log(value); } [OK]
Hint: Use !== null && !== undefined for safe truthiness checks [OK]
Common Mistakes:
Using assignment (=) instead of comparison (===)
Checking for falsy instead of truthy
Confusing equality operators
3. What will be the output of this TypeScript code?
function printLength(str?: string) {
if (str) {
console.log(str.length);
} else {
console.log('No string');
}
}
printLength('hello');
printLength('');
printLength();
medium
A. 5, No string, No string
B. 5, 0, undefined
C. 5, No string, undefined
D. 5, 0, No string
Solution
Step 1: Analyze the if condition with truthiness narrowing
The condition if (str) is true only if str is truthy. An empty string ('') is falsy, so it goes to else.
Step 2: Check each function call output
printLength('hello') prints 5 because 'hello' is truthy and length is 5. printLength('') prints 'No string' because '' is falsy. printLength() prints 'No string' because str is undefined (falsy).
Final Answer:
5, No string, No string -> Option A
Quick Check:
Falsy empty string triggers else [OK]
Hint: Empty string is falsy, so else block runs [OK]
Common Mistakes:
Assuming empty string is truthy
Expecting undefined to print length
Confusing falsy values with null only
4. Find the error in this TypeScript code using truthiness narrowing: