Bird
Raised Fist0
Typescriptprogramming~20 mins

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

Choose your learning style10 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.

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

  1. 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.
  2. Step 2: Compare with other types

    Other options describe common types like any, null, or optional, which are different from never.
  3. Final Answer:

    A function that never returns normally, like one that always throws an error or loops forever. -> Option D
  4. 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

  1. Step 1: Check function return type and body

    A function returning never must not return normally. Throwing an error fits this.
  2. 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.
  3. Final Answer:

    function fail(): never { throw new Error('Fail'); } -> Option A
  4. 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

  1. Step 1: Analyze the switch cases

    The function handles 'string' and 'number' types explicitly, so the default case is unreachable for these inputs.
  2. 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.
  3. Final Answer:

    'number' -> Option B
  4. 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

  1. Step 1: Understand the function purpose

    assertNever is used to catch unexpected values at compile time by requiring a never type.
  2. 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.
  3. Final Answer:

    No error; code is correct and safe. -> Option C
  4. 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

  1. Step 1: Identify missing case problem

    The 'square' case is missing, so the switch is not exhaustive.
  2. 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.
  3. Final Answer:

    Add a default case that calls a function accepting never to catch unhandled cases. -> Option A
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard