📖 Scenario: You are building a simple program that checks user input values and decides if they are valid or not based on whether they are "truthy" or "falsy" in TypeScript.
🎯 Goal: Learn how to use truthiness narrowing to check if a variable has a meaningful value before using it.
📋 What You'll Learn
Create a variable with a value that can be truthy or falsy
Create a boolean variable to hold the check result
Use an if statement to narrow the type by checking truthiness
Print the result to the console
💡 Why This Matters
🌍 Real World
Checking if user inputs or data values exist before using them is common in web forms and applications.
💼 Career
Understanding truthiness narrowing helps avoid errors and write safer TypeScript code in real projects.
Progress0 / 4 steps
1
Create a variable with a value that can be truthy or falsy
Create a variable called userInput and assign it the value "Hello".
Typescript
Hint
Use let userInput: string | null = "Hello"; to allow userInput to be a string or null.
2
Create a boolean variable to hold the check result
Create a boolean variable called isValid and set it to false.
Typescript
Hint
Use let isValid: boolean = false; to create the boolean variable.
3
Use an if statement to narrow the type by checking truthiness
Use an if statement to check if userInput is truthy. Inside the if, set isValid to true.
Typescript
Hint
Use if (userInput) { isValid = true; } to check truthiness and update isValid.
4
Print the result to the console
Write a console.log statement to print the value of isValid.
Typescript
Hint
Use console.log(isValid); to show the result.
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: