“Type is not assignable to type ‘never'” In TypeScript – How To Fix?

Type is not assignable to type ‘never’ in TypeScript

The “Type is not assignable to type ‘never'” error in TypeScript often occurs when our array is empty and has the type of ‘never’. Please follow our instructions below to find out how to fix this problem.

What causes the “Type is not assignable to type ‘never'” error in TypeScript?

Reproduce the “Type is not assignable to type ‘never'” error in TypeScript

Example 1:

const myArray = [];

myArray[0] = 1;
myArray[1] = 2;

console.log(myArray[0]);
console.log(typeof myArray[0]);
console.log(myArray[1]);
console.log(typeof myArray[1]);

As we run the codes, we will get this error: 

Type 'number' is not assignable to type 'never'.ts(2322)

The reason for this is that we didn’t define our array type. Therefore, our array gets the type of ‘never’. A variable with type ‘never’ will have no value, and also, you cannot assign a value to it. 

Example 2:

var a: never
a = 'Welcome to LearnShareIt.com'
console.log(a)

Output:

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

How to fix this error?

Solution 1: Setting type to the array

The easiest way to fix this problem is to set explicitly type to our variable. For example 1, we will set type for the array as ‘number’. 

Fix code for example 1: 

const myArray: number[] = [];

myArray[0] = 1;
myArray[1] = 2;

console.log(myArray[0]);
console.log(typeof myArray[0]);
console.log(myArray[1]);
console.log(typeof myArray[1]);

Output:

1
number
2
number

Solution 2: Adjust the ‘tsconfig.json’ file 

Another thing we can do to solve this problem is to adjust the tsconfig.json file so that every time we do not set explicitly type the value of our array, it will get the type of any instead of never.

Here is an example of the tsconfig.json file:

{
    "compilerOptions": {
        "module": "commonjs",
        "noImplicitAny": false,
        "removeComments": true,
        "preserveConstEnums": true,
        "sourceMap": true
        "strictNullChecks": true,
     }
}

For our problem, we only need to consider the “noImplicitAny” and “strictNullChecks“. 

If we set: 

"noImplicitAny": false	 
"strictNullChecks": true

then our empty array will be automatically set to type ‘never‘.

But if we set them like this: 

"noImplicitAny": true
"strictNullChecks": true

Then our empty array will be automatically set to type ‘any‘.

And since its type is ‘any’, it can contain any type of element.

Let’s rerun example 1:

const myArray = [];

myArray[0] = 1;
myArray[1] = 2;

console.log(myArray[0]);
console.log(typeof myArray[0]);
console.log(myArray[1]);
console.log(typeof myArray[1]);

Output:

1
number
2
number

Summary

The “Type is not assignable to type ‘never'” error in TypeScript often occurs when our variable gets the type of ‘any’ and cannot contain a value. The solutions are to explicitly type our variable or adjust the tsconfig.json file so that our variable will get the type of ‘any‘.

Maybe you are interested:

Leave a Reply

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