The never type represents values that never occur. It is used for functions that never return or variables that cannot have any value.
Click to reveal answer
beginner
When does TypeScript consider code to be unreachable?
Code is unreachable if it can never be executed, for example, code after a function that always throws an error or an infinite loop.
Click to reveal answer
beginner
What happens if a function has a return type of never?
It means the function never finishes normally. It either throws an error or runs forever, so it never returns a value.
Click to reveal answer
intermediate
Why is the never type useful for exhaustive checks?
It helps catch missing cases in code like switch statements by making sure all possibilities are handled, otherwise TypeScript shows an error.
Click to reveal answer
beginner
Give an example of unreachable code in TypeScript.
Example: <pre>function example() { return; console.log('unreachable'); }</pre> The <code>console.log</code> line is unreachable because the function returns before it.
Click to reveal answer
What does the never type represent in TypeScript?
AA value that never occurs
BA value that can be any type
CA value that is optional
DA value that is null
✗ Incorrect
The never type means a value that never happens, like a function that never returns.
Which of these is an example of unreachable code?
ACode inside an if statement
BCode inside a function
CCode inside a loop
DCode after a return statement
✗ Incorrect
Code after a return statement is unreachable because the function stops running at return.
What will a function with return type never do?
AReturn a string
BNever return normally
CReturn a number
DReturn null
✗ Incorrect
A never function never returns normally; it throws or loops forever.
How does TypeScript help with unreachable code?
AIt shows an error or warning for unreachable code
BIt converts unreachable code to comments
CIt deletes unreachable code automatically
DIt ignores unreachable code
✗ Incorrect
TypeScript warns you when code can never be reached, helping you find mistakes.
Why use never in exhaustive checks?
ATo allow missing cases
BTo ignore errors
CTo catch missing cases at compile time
DTo make code run faster
✗ Incorrect
Using never forces you to handle all cases, so TypeScript catches missing ones.
Explain what the never type means and give an example of a function that returns never.
Think about functions that never finish normally.
You got /3 concepts.
Describe what unreachable code is and why TypeScript warns about it.
Consider what happens after a return statement.
You got /3 concepts.
Practice
(1/5)
1. What does the never type represent in TypeScript?
easy
A. A variable that can hold any type of value.
B. A type that represents optional values.
C. A function that returns null or undefined.
D. A function that never returns normally, like one that always throws an error or loops forever.
Solution
Step 1: Understand the never type meaning
The never type is used for functions or expressions that never complete normally, such as those that throw errors or run infinite loops.
Step 2: Compare with other types
Other options describe common types like any, null, or optional, which are different from never.
Final Answer:
A function that never returns normally, like one that always throws an error or loops forever. -> Option D
Quick Check:
never means no normal return [OK]
Hint: Never means function never finishes normally [OK]
Common Mistakes:
Confusing never with void or any
Thinking never means optional or nullable
Assuming never is a value type
2. Which of the following is the correct syntax for a function that returns never in TypeScript?
easy
A. function fail(): never { throw new Error('Fail'); }
B. function fail(): void { return; }
C. function fail(): never { return null; }
D. function fail(): string { throw new Error('Fail'); }
Solution
Step 1: Check function return type and body
A function returning never must not return normally. Throwing an error fits this.
Step 2: Validate each option
function fail(): never { throw new Error('Fail'); } throws an error, so it never returns normally. function fail(): void { return; } returns void normally. function fail(): never { return null; } tries to return null, which is invalid for never. function fail(): string { throw new Error('Fail'); } returns string but throws, so return type mismatches.
Final Answer:
function fail(): never { throw new Error('Fail'); } -> Option A
Quick Check:
Throw error matches never return [OK]
Hint: Throw error to return never type correctly [OK]
Common Mistakes:
Returning a value in a never function
Using void instead of never
Returning null or undefined in never
3. What will be the output of this TypeScript code?
function check(value: string | number) {
switch (typeof value) {
case 'string':
return 'string';
case 'number':
return 'number';
default:
const _exhaustiveCheck: never = value;
return _exhaustiveCheck;
}
}
console.log(check(42));
medium
A. 'string'
B. 'number'
C. Compilation error due to unreachable code
D. Runtime error
Solution
Step 1: Analyze the switch cases
The function handles 'string' and 'number' types explicitly, so the default case is unreachable for these inputs.
Step 2: Check the call and output
Calling check(42) matches the 'number' case, so it returns 'number'. The default case with never is never reached.
Final Answer:
'number' -> Option B
Quick Check:
Handled all cases, returns 'number' [OK]
Hint: Check switch covers all types, default never runs [OK]
Common Mistakes:
Thinking default case runs for known types
Confusing never with runtime errors
Expecting compilation error for unreachable default
4. Identify the error in this TypeScript code snippet:
function assertNever(x: never): never {
throw new Error('Unexpected value: ' + x);
}
function process(value: string | number) {
if (typeof value === 'string') {
return 'string';
} else if (typeof value === 'number') {
return 'number';
} else {
return assertNever(value);
}
}
medium
A. Error because assertNever should return void not never.
B. Error because assertNever is called with a non-never type.
C. No error; code is correct and safe.
D. Error because value can never be string | number.
Solution
Step 1: Understand the function purpose
assertNever is used to catch unexpected values at compile time by requiring a never type.
Step 2: Analyze the process function
All possible types string and number are handled. The else branch is unreachable, so value is never there, making the call valid.
Final Answer:
No error; code is correct and safe. -> Option C
Quick Check:
Unreachable else means value is never [OK]
Hint: Else unreachable means value is never type [OK]
Common Mistakes:
Thinking assertNever is called with string or number
Confusing never return with void
Assuming else branch always runs
5. Given this TypeScript code, what is the best way to ensure all cases are handled and catch missing cases at compile time?
type Shape = { kind: 'circle', radius: number } | { kind: 'square', size: number };
function area(shape: Shape): number {
switch (shape.kind) {
case 'circle':
return Math.PI * shape.radius ** 2;
// Missing case for 'square'
}
}
hard
A. Add a default case that calls a function accepting never to catch unhandled cases.
B. Ignore missing cases; TypeScript will warn automatically.
C. Add a default case that returns 0 without checks.
D. Use any type for shape to avoid errors.
Solution
Step 1: Identify missing case problem
The 'square' case is missing, so the switch is not exhaustive.
Step 2: Use never type to catch missing cases
Adding a default case that calls a function requiring never forces TypeScript to check exhaustiveness and report missing cases.
Final Answer:
Add a default case that calls a function accepting never to catch unhandled cases. -> Option A
Quick Check:
Use never in default to enforce exhaustiveness [OK]
Hint: Use default with never to catch missing cases [OK]
Common Mistakes:
Assuming TypeScript warns without default never
Returning default values silently
Using any to bypass type safety
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.