0
0
Typescriptprogramming~20 mins

Never type and unreachable code in Typescript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Never Type Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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'));
A"Got a"
BTypeError at runtime
CCompilation error due to never type assignment
D"Got b"
Attempts:
2 left
💡 Hint
The function handles all possible cases of the union type, so the default case is never reached.
Predict Output
intermediate
2: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);
A0
B1
Cundefined
D2
Attempts:
2 left
💡 Hint
The function always throws an error, so the code after calling it inside try is unreachable.
🔧 Debug
advanced
2: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');
}
ALine with <code>return input.toUpperCase();</code> because input might not be string
BLine with <code>return input.toFixed(2);</code> because input might not be number
CLine with <code>console.log('Unreachable code');</code> because all cases are handled
DNo unreachable code, all lines can run
Attempts:
2 left
💡 Hint
The input type is a union of string or number, and both are handled with returns.
📝 Syntax
advanced
2: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?
A
function example(x: string | number) {
  if (typeof x === 'string') {
    return x;
  } else if (typeof x === 'number') {
    return x;
  } else {
    const y: never = x;
    return y;
  }
}
B
function example(x: string | number) {
  if (typeof x === 'string') {
    return x;
  } else {
    const y: never = x;
    return y;
  }
}
C
function example(x: string | number) {
  if (typeof x === 'string') {
    return x;
  } else if (typeof x === 'number') {
    return x;
  }
}
D
function example(x: never) {
  return x;
}
Attempts:
2 left
💡 Hint
Check if all possible types are handled before assigning to never.
🚀 Application
expert
3: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';
}
A2
B1
C0
D3
Attempts:
2 left
💡 Hint
Think about which lines can never be reached because all cases are handled.