Sling Academy
Home/TypeScript/TypeScript error: Not assignable to type ‘never’

TypeScript error: Not assignable to type ‘never’

Last updated: January 08, 2024

Overview

When working with TypeScript, you might encounter the not assignable to type 'never' error. This can be a puzzling message if you’re not familiar with TypeScript’s type system. In this article, we’ll explore the reasons behind this error and provide practical solutions to fix it.

Possible Causes

The error springs up in contexts where TypeScript’s compiler deduces a variable type to be never, which is a type that represents the absence of any value and is typically assigned when a function never returns. When you assign a value to a variable that TypeScript deduces should be of type never, you get the error.

Common scenarios include:

  • Improper use of type narrowing or type guards.
  • Function return types in situations where a function never returns.
  • Issues with complex union or intersection types.

Solutions

Solution 1: Check Type Narrowing Logic

Incorrectly implemented type narrows can lead to never type assignments.

  1. Identify the scope where the never error is reported.
  2. Analyze type narrowing logic such as if blocks and type guard functions.
  3. Ensure conditions are not contradictory or overly restrictive.
  4. Adjust the logic to correctly reflect the possible types.

Example:

function example(value: string | number) {
  if (typeof value === 'string') {
    // String logic
  }
  else if (typeof value === 'number') {
    // Number logic
  }
  // Make sure there's no else block implying 'never'
}

The main advantage of this approach is the avoidance of potential runtime errors due to safer type checking.

Solution 2: Expand Union Types

Expand the union type to include the type you are trying to assign.

  1. Locate the union type causing the issue.
  2. Include the type you intend to assign to the union.
  3. Refactor any associated logic that might be affected by the update.

Example:

let value: string | number;
value = 'Hello, World'; // No error

This ensures proper type settings. However, widening types could cause issues if values aren’t correctly handled at runtime.

Solution 3: Utilizing Type Assertions

Explicitly define an expected type using type assertions.

  1. Pinpoint where the error occurs.
  2. Use a type assertion to specify the expected type.

Example:

let value: never;
value = 'Hello, World' as unknown as string; // Type assertion

Type assertions offer more control but come with the risk of bypassing TypeScript’s safety checks if used improperly.

Next Article: Using Nested Types and Nested Interfaces in TypeScript

Previous Article: TypeScript: How to Convert Object to Array and Vice-Versa

Series: TypeScript: Intermediate & Advanced Tutorials

TypeScript

You May Also Like

  • TypeScript: setInterval() and clearInterval() methods (3 examples)
  • TypeScript sessionStorage: CRUD example
  • Using setTimeout() method with TypeScript (practical examples)
  • Working with window.navigator object in TypeScript
  • TypeScript: Scrolling to a specific location
  • How to resize the current window in TypeScript
  • TypeScript: Checking if an element is a descendant of another element
  • TypeScript: Get the first/last child node of an element
  • TypeScript window.getComputerStyle() method (with examples)
  • Using element.classList.toggle() method in TypeScript (with examples)
  • TypeScript element.classList.remove() method (with examples)
  • TypeScript: Adding Multiple Classes to An Element
  • element.insertAdjacentHTML() method in TypeScript
  • TypeScript – element.innerHTML and element.textContent
  • Using element.removeAttribute() method in TypeScript
  • Working with Document.createElement() in TypeScript
  • Using getElementById() method in TypeScript
  • Using Window prompt() method with TypeScript
  • TypeScript – window.performance.measure() method (with examples)