What is the purpose of using never in TypeScript for exhaustive checking?
The never type is used to ensure that all possible cases in a union type are handled. If a case is missed, TypeScript will show an error, helping catch bugs early.
Click to reveal answer
intermediate
How do you use never in a switch statement for exhaustive checking?
You add a default case that assigns the variable to never. This makes TypeScript check if any case is missing and gives an error if so.
Click to reveal answer
beginner
What happens if you forget to handle a case in a union type when using exhaustive checking with never?
TypeScript will produce a compile-time error, telling you that the variable is not of type never. This means you missed a case and need to handle it.
Click to reveal answer
intermediate
Explain the role of the assertNever function in exhaustive checking.
The assertNever function takes a value of type never. It is called in the default case to make sure no other cases exist. If a new case is added but not handled, TypeScript will error.
Click to reveal answer
beginner
Why is exhaustive checking with never useful in real projects?
It helps catch missing cases early, making code safer and easier to maintain. It prevents bugs caused by unhandled values in union types.
Click to reveal answer
What type does the never type represent in TypeScript?
AA type for any value
BA type for numbers only
CA type for strings only
DA type that never occurs
✗ Incorrect
never means a value that never happens, used to catch impossible cases.
In exhaustive checking, where is the never type usually used?
AIn the first case of a <code>switch</code>
BIn the <code>default</code> case of a <code>switch</code> statement
COnly in function return types
DIn variable declarations
✗ Incorrect
The never type is used in the default case to catch unhandled cases.
What does TypeScript do if a case is missing when using exhaustive checking with never?
ARuns the program anyway
BIgnores the missing case
CShows a compile-time error
DConverts the missing case to <code>any</code>
✗ Incorrect
TypeScript shows an error to help you fix the missing case.
What is the purpose of an assertNever function?
ATo check that a value is of type <code>never</code>
BTo convert a value to <code>never</code>
CTo return a string
DTo handle all cases explicitly
✗ Incorrect
assertNever ensures the value is never, helping with exhaustive checks.
Which of these is a benefit of exhaustive checking with never?
ACatches missing cases early
BMakes code run faster
CAllows any type without errors
DRemoves the need for <code>switch</code> statements
✗ Incorrect
It helps find missing cases before running the program.
Explain how the never type helps with exhaustive checking in TypeScript.
Think about how TypeScript checks all options in a union.
You got /3 concepts.
Describe the role and structure of an assertNever function in exhaustive checking.
It's a helper function to make sure no cases are missed.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of using never in exhaustive checking in TypeScript?
easy
A. To declare a variable that can hold any value
B. To allow any type of value without restrictions
C. To ignore errors during compilation
D. To ensure all possible cases are handled in a switch or conditional
Solution
Step 1: Understand the role of never in exhaustive checks
never is used to represent values that should never occur, helping catch unhandled cases.
Step 2: Connect never usage to exhaustive case handling
By forcing a function to accept never, TypeScript ensures all cases are covered, or else a compile error occurs.
Final Answer:
To ensure all possible cases are handled in a switch or conditional -> Option D
Quick Check:
Exhaustive checking = catch all cases [OK]
Hint: Use never to catch missing cases in switch statements [OK]
Common Mistakes:
Thinking never allows any value
Using never to ignore errors
Confusing never with unknown type
2. Which of the following is the correct syntax for a helper function that uses never to enforce exhaustive checking?
easy
A. function assertNever(x: never): never { throw new Error('Unexpected value: ' + x); }
B. function assertNever(x: any): void { console.log(x); }
C. function assertNever(x: unknown): never { return x; }
D. function assertNever(x: never): void { return; }
Solution
Step 1: Identify correct parameter and return types
The helper function must accept a parameter of type never and return never because it throws an error.
Step 2: Check function body matches expected behavior
The function throws an error with the unexpected value, which is the standard pattern for exhaustive checks.
Final Answer:
function assertNever(x: never): never { throw new Error('Unexpected value: ' + x); } -> Option A
Quick Check:
Helper function with never param and throw = correct [OK]
Hint: Helper must take never and throw error to enforce checks [OK]
Common Mistakes:
Using any or unknown instead of never
Not throwing an error inside the function
Returning void instead of never
3. What will be the output of this TypeScript code?
type Shape = 'circle' | 'square';
function area(shape: Shape) {
switch (shape) {
case 'circle':
return 3.14 * 2 * 2;
// Missing case for 'square'
default:
return assertNever(shape);
}
}
function assertNever(x: never): never {
throw new Error('Unexpected shape: ' + x);
}
console.log(area('square'));
medium
A. Returns 12.56
B. Throws Error: Unexpected shape: square
C. Returns undefined
D. Compilation error due to missing case
Solution
Step 1: Analyze switch cases and input
The function handles 'circle' but not 'square', so at runtime, calling area('square') hits the default case.
Step 2: Understand assertNever behavior at runtime
The assertNever function throws an error with the unexpected shape, so the program throws a runtime error.
Final Answer:
Throws Error: Unexpected shape: square -> Option B
Quick Check:
Missing case causes runtime error when called [OK]
Hint: Missing case triggers runtime error when called [OK]
Common Mistakes:
Assuming missing case returns undefined
Thinking it returns a number for all inputs
Expecting compile error instead of runtime error
4. Identify the error in this TypeScript code using exhaustive checking:
type Direction = 'up' | 'down' | 'left';
function move(dir: Direction) {
switch (dir) {
case 'up':
return 'Moving up';
case 'down':
return 'Moving down';
default:
return assertNever(dir);
}
}
function assertNever(x: never): never {
throw new Error('Unexpected direction: ' + x);
}
medium
A. Missing case for 'left' in switch statement
B. assertNever function has wrong parameter type
C. Switch statement should not have a default case
D. Function move should return void instead of string
Solution
Step 1: Check all union cases in switch
The type Direction has 'up', 'down', and 'left', but 'left' is missing in the switch.
Step 2: Understand impact of missing case with assertNever
Missing 'left' causes default to call assertNever with a string, which is not never, causing a compile error.
Final Answer:
Missing case for 'left' in switch statement -> Option A
Quick Check:
All union cases must be handled [OK]
Hint: Check all union cases explicitly to avoid errors [OK]
Common Mistakes:
Ignoring missing union cases
Thinking default case covers all safely
Wrong assertNever parameter type
5. Given this code, how can you modify it to ensure exhaustive checking for all Status values?
type Status = 'success' | 'error' | 'loading';
function handleStatus(status: Status) {
switch (status) {
case 'success':
return 'Done';
case 'error':
return 'Failed';
// Missing 'loading' case
default:
return assertNever(status);
}
}
function assertNever(x: never): never {
throw new Error('Unexpected status: ' + x);
}
hard
A. Change the type Status to include only 'success' and 'error'
B. Remove the default case and rely on TypeScript errors
C. Add a case for 'loading' in the switch statement
D. Change assertNever parameter type to any
Solution
Step 1: Identify missing union case in switch
The 'loading' case is missing, so the switch is not exhaustive.
Step 2: Add missing case to ensure all Status values are handled
Adding case 'loading': with appropriate return ensures exhaustive checking and no runtime errors.
Final Answer:
Add a case for 'loading' in the switch statement -> Option C
Quick Check:
All union members must have cases [OK]
Hint: Add missing union cases to switch for full coverage [OK]
Common Mistakes:
Removing default case without adding missing cases
Changing type to avoid handling all cases
Using any instead of never in assertNever
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.