Logo

What is "not assignable to parameter of type never" error in TypeScript?

The “not assignable to parameter of type ‘never’” error in TypeScript typically happens when you try to do something with an array or variable that the compiler has inferred to have the type never. This often occurs if TypeScript believes a variable should never actually hold any valid values—so trying to assign or push anything into it causes an error.

1. Why Does a Variable Become never?

A never type in TypeScript means “this value can never occur.” Some common scenarios where this might show up:

  1. Empty Array Inference

    • If you do const arr = []; without any further context, TypeScript might infer arr as never[] (an array that shouldn’t accept any items).
    • This is especially likely when --strictNullChecks or --strict mode is on, and TypeScript has no hints about what you intend to store in that array.
  2. Wrong Generic Usage

    • If you have a function with generics that never get inferred properly, the result might be never.
  3. Discriminated Union Exhaustiveness

    • In a switch statement over a union type, if TypeScript is sure you’ve covered all cases, leftover branches might be never.

Example: Empty Array

const arr = []; // TypeScript might infer: let arr: never[] arr.push("Hello"); // Error: Argument of type 'string' is not assignable to parameter of type 'never'.

To fix this, explicitly type or provide context:

const arr: string[] = []; arr.push("Hello"); // okay

2. Understanding the Error

“Argument of type 'X' is not assignable to parameter of type 'never'” basically means:

“TypeScript has determined this parameter must be of type never. Hence, you can’t pass any actual value to it.”

Because never in TypeScript is a type that can’t hold any valid value, any attempt to assign something to it fails.

3. How to Fix It

  1. Provide an Explicit Type

    • If you need an array, do let arr: SomeType[] = [].
    • If you need a function param or variable, specify the type so it’s not inferred as never.
  2. Check Your Inference

    • Ensure you’re not initializing a variable in a way that gives the compiler no clues.
    • Provide a default or a more generic type.
  3. Check Your Generic Constraints

    • If you have a function like function doSomething<T>(x: T[]) { ... }, ensure TypeScript can infer T.
    • If T ends up as never, it means the compiler sees no usage that hints at a real type.

4. Example Fixes

4.1 Explicitly Type an Empty Array

// Problematic const arr = []; arr.push(123); // error: Type 'number' is not assignable to type 'never' // Solution const arr2: number[] = []; arr2.push(123); // OK

4.2 Provide a Default or a Union

type Something = string | number; // Without context, might become never: let data = []; data.push(42); // error // Provide context: let data2: Something[] = []; data2.push(42); // OK data2.push("hello"); // also OK

5. Summary

  • never means “this can never happen” or “no valid value.”
  • If you see “Argument of type ‘X’ is not assignable to parameter of type ‘never’”, it often means your variable (or array) has inadvertently become never[].
  • Fix by specifying an explicit type or providing usage context so TypeScript can correctly infer a meaningful type.

Also, if you want to strengthen your JavaScript fundamentals (which TypeScript builds on), consider the Grokking JavaScript Fundamentals course by DesignGurus.io. It covers core JS concepts like prototypes, closures, and async programming—knowledge that translates directly to more effective TypeScript usage.

CONTRIBUTOR
TechGrind