How To Fix “Argument of type not assignable to parameter type ‘never'”

Argument of type not assignable to parameter type ‘never’

The error “Argument of type not assignable to parameter type ‘never'” can be unfamiliar and hard to understand when you have just come from JavaScript. This guide will introduce the never type and why it can cause such issues.

Reproduce the error

Let’s declare an array (Without indicating the type of its element), and then assign any value to one of its elements. This is possible a error-free in JavaScript. However, TypeScript will produce an error instead:

let arr = [];
arr[1] = 2022;

Output:

Type 'number' is not assignable to type 'never'.

In the next example, we create an object type to represent a website, storing its name and URL. When we assign values to these keys, the compiler raises similar errors:

let site = {
    name: [],
    URL: [],
};
site.name[0] = 'LearnShareIT';
site.URL[0] = 'learnshareit.com';

Output:

Type 'string' is not assignable to type 'never'.
Type 'string' is not assignable to type 'never'.

To know why those errors occur, you need to understand what the never type is in TypeScript.

The ‘never’ type

TypeScript is a subset of JavaScript. But unlike JavaScript, TypeScript supports optional static typing and comes with an explicit type system. It is designed to strictly enforce data types.

Your TypeScript scripts will be compiled to JavaScript eventually so browser and runtime engines like Node.js can understand them. But the compiler will check the types of your variables. This compile-time type verification is a form of optimization and helps you provide certain errors.

There are many common types, which are based on primitives and objects in JavaScript, such as string and number. They define the set of values you can assign to each variable.

But TypeScript also provides special types like never, which have useful use if you can take advantage of them. In particular, the never type means you should never assign anything to variables of that type. This also means when the variable has the never type, it can’t have any value.

You might not find yourself using this type a lot. But the never type can represent many things in TypeScript, including but not limited to empty unions, inadmissible parameters, and the return type of functions.

In our examples, the compiler will give the empty array literals ([]) the never type (never[]). That is why it raises an error when you attempt to assign a value to them.

This error also occurs when you set the return type of a function to never but return a value in the code afterward:

function greet(a: string): never {
    while (true) {
        console.log(a);
    }

    return a;
}

Output:

Type 'string' is not assignable to type 'never'.

Solutions for error “Argument of type not assignable to parameter type ‘never'”

The best way to get rid of those errors is to explicitly declare the type for your variables and objects (as well as assign the correct type to them).

Example:

let arr: number[] = [];
arr[1] = 2022;
interface Site {
    name: string;
    URL: string;
}

let site: Site = {
    name: 'LearnShareIT',
    URL: 'learnshareit.com',
}

console.log(arr[1]);
console.log(site.name);
console.log(site.URL);

Output:

2022
LearnShareIT
learnshareit.com

In the third example, just remove the return statement, and your code will be compiled without issues:

function greet(a: string): never {
    while (true) {
        console.log(a);
    }
}

greet('LearnShareIT');

Output:

LearnShareIT
LearnShareIT
LearnShareIT

Note: This is an infinite look. You can press Ctrl+C in the console to terminate it.

Summary

The error “Argument of type not assignable to parameter type ‘never'” occurs when you try to assign a value to a variable of the never type. Explicitly setting that variable to another type will solve the issue. Understanding this can also help you with other errors in TypeScript like “Type ‘string | undefined’ is not assignable to type ‘string'” error, you can see that error here.

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *