. Advertisement .
..3..
. Advertisement .
..4..
You may run into the error Type is not assignable to type ‘never’ in TypeScript when you don’t pay much attention to the ‘never’ type. Learn more about this special type and how to fix the error with this guide.
Type is not assignable to type ‘never’ in TypeScript
Reproduce The Error
We are going to use a type alias to declare the type and initialize a value of this type.
type Site = {
name: string;
rank: number;
URL?: string;
};
const site0: Site = {
name: 'ITTutoria',
rank: 1,
URL: "https://ittutoria.net",
}
This code is written to assign properties of the above variable to two arrays:
const sitename = [];
sitename[0] = site0.name;
const siterank = [];
siterank[0] = site0.rank;
console.log(sitename[0]);
console.log(siterank[0]);
But we receives these errors instead:
Type 'string' is not assignable to type 'never'.
Type 'number' is not assignable to type 'never'.
The never Type
Available since version 2.0, the never type doesn’t receive as much discussion as other types in TypeScript. But it is an important implicit type the compiler may assign to your values, especially when narrowing. And since TypeScript has strong type checking, you may end up with errors like what the above code produces.
In particular, the TypeScript type of a variable is just a set of possible values for it. For instance, ‘string’ and ‘number’ types represent two infinite sets of values you can give for variables of those types.
In the case of the ‘never’ type, this set of values is empty. TypeScript implements the never type when you want to make sure there is no possibility that a certain variable can have any value.
You can’t assign any value of any other type to it, except never itself. On the other hand, it is a subtype and can be assigned to any type.
There are plenty of applications of this type in your code. For instance, you can use variables of the never type to present Inadmissible parameters in functions and generics. Empty unions are another common application.
You can also use this type for functions that should not have a returned value. In those cases, the compiler will infer the return type as never. You will see this inference with:
- Arrow functions and functions with no annotations for the return type
- Functions with no return statements
- Functions with only returns statements of the never type
- Functions whose endpoints couldn’t be reached by the control flow analysis.
Since it is a subtype of every TypeScript type, the compiler always omits ‘never’ from union types and returns type inferences where other types are being returned as well.
How To Fix The Error
The errors in our code occur because as you don’t declare the type for your arrays, TypeScript may assume it is ‘never’. That is why the statements that assign values of ‘string’ and ‘number’ to it produce such errors.
To get rid of these messages, you can explicitly declare the expected types for your arrays:
const sitename: string[] = [];
sitename[0] = site0.name;
const siterank: number[] = [];
siterank[0] = site0.rank;
console.log(sitename[0]);
console.log(siterank[0]);
The code should now run without issues:
ITTutoria
1
Note: follow this guide if you want to format strings in TypeScript.
Summary
The error Type is not assignable to type ‘never’ in TypeScript happens when you assign values to a variable the compiler has inferred that has the ‘never’ type. You can declare the correct type for the variable to solve the problem.
Leave a comment