typescript type_narrowing_error ai_generated true

TS2339: Property 'X' does not exist on type 'never'

ID: typescript/ts-never-type-error

Also available as: JSON · Markdown
85%Fix Rate
87%Confidence
50Evidence
2023-01-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
5 active

Root Cause

A variable has been narrowed to never, meaning TypeScript thinks the code is unreachable. Usually caused by incomplete type guards.

generic

Workarounds

  1. 90% success Add an exhaustive check helper for discriminated unions
    function assertNever(x: never): never {
      throw new Error(`Unexpected value: ${x}`);
    }
    
    switch (action.type) {
      case 'add': return handleAdd(action);
      case 'remove': return handleRemove(action);
      default: return assertNever(action); // catches missing cases
    }

    Sources: https://www.typescriptlang.org/docs/handbook/2/narrowing.html#exhaustiveness-checking

  2. 87% success Fix the type narrowing that incorrectly narrows to never
    // If an array filter narrows incorrectly:
    const items = arr.filter((x): x is ValidType => x !== null);
    // Use type predicate to maintain correct type

    Sources: https://www.typescriptlang.org/docs/handbook/2/narrowing.html#using-type-predicates

Dead Ends

Common approaches that don't work:

  1. Assert the variable as any to suppress the error 75% fail

    The never type usually indicates a logic error; suppressing it hides real bugs

Error Chain

Leads to:
Preceded by:
Frequently confused with: