Challenge - 5 Problems
Never Type Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of function with never type in switch
What is the output of this TypeScript code when calling
checkValue('a')?Typescript
function checkValue(x: 'a' | 'b'): string { switch (x) { case 'a': return 'Got a'; case 'b': return 'Got b'; default: const _exhaustiveCheck: never = x; return _exhaustiveCheck; } } console.log(checkValue('a'));
Attempts:
2 left
💡 Hint
The function handles all possible cases of the union type, so the default case is never reached.
✗ Incorrect
The function covers all possible values of the union type 'a' | 'b'. The default case is unreachable, so the never type assignment is valid and the function returns 'Got a' when called with 'a'.
❓ Predict Output
intermediate2:00remaining
Value of variable after function with never return
What is the value of
result after running this code?Typescript
function fail(): never { throw new Error('Fail'); } let result = 0; try { fail(); result = 1; } catch { result = 2; } console.log(result);
Attempts:
2 left
💡 Hint
The function always throws an error, so the code after calling it inside try is unreachable.
✗ Incorrect
The function fail() never returns normally because it throws an error. So the assignment result = 1 is never executed. The catch block sets result to 2.
🔧 Debug
advanced2:00remaining
Identify unreachable code in function with never
Which line in this function is unreachable and why?
Typescript
function process(input: string | number) { if (typeof input === 'string') { return input.toUpperCase(); } else if (typeof input === 'number') { return input.toFixed(2); } console.log('Unreachable code'); }
Attempts:
2 left
💡 Hint
The input type is a union of string or number, and both are handled with returns.
✗ Incorrect
Since input can only be string or number, both cases return a value. The last console.log line is never reached, so it is unreachable code.
📝 Syntax
advanced2:00remaining
Which option causes a TypeScript error due to never type?
Which of these code snippets causes a TypeScript compilation error because of incorrect use of the never type?
Attempts:
2 left
💡 Hint
Check if all possible types are handled before assigning to never.
✗ Incorrect
In option B, the else block can receive a number, but it tries to assign it to a variable of type never, which causes a compilation error. Option B handles all cases and uses never correctly. Option B returns for all cases and has no unreachable code. Option B is valid because x is never type.
🚀 Application
expert3:00remaining
How many unreachable lines are in this function?
Consider this TypeScript function. How many lines are unreachable?
Typescript
function analyze(value: 'x' | 'y' | 'z') { switch (value) { case 'x': return 'X'; case 'y': return 'Y'; case 'z': return 'Z'; default: const check: never = value; break; } console.log('After switch'); return 'Done'; }
Attempts:
2 left
💡 Hint
Think about which lines can never be reached because all cases are handled.
✗ Incorrect
The default case is unreachable because all possible values are handled. The break in default is unreachable. Also, the console.log and return after switch are unreachable because all cases return. So two lines are unreachable: the break and the console.log line.