. Advertisement .
..3..
. Advertisement .
..4..
“Argument of type not assignable to parameter type ‘never‘” is a confusing problem that shows up in many ways. What is the cause of this error and how to solve it? Read on this article, we will give you some solutions to fix this error.
When Do You Get The Error “Argument of type not assignable to parameter type ‘never'”?
Each type has a range of potential values. A string type, for instance, can take any of an unlimited number of different forms. As a result, if we annotate a variable with the type string, it can only take values from that set, i.e., strings:
let foo: string = 'foo'
foo = 3 // the set of strings does not have number
Never
is an empty set of values in TypeScript. A similar type is an empty in Flow, another well-known JavaScript type system.
The never
type can never have any value, including values of any
type because there are no values in the set. Because of this, never
is also occasionally referred to as a bottom type or an uninhabitable type.
When you run the program, you easily get the error “Argument of type not assignable to parameter type ‘never'”. Take a look at the error details example below:
function fn(input: never) {}
// it only accepts `never`
declare let myNever: never
fn(myNever)
// passing anything else (or nothing) causes a type error
fn() // Error: An argument for 'input' was not provided.
fn(1) // Error: Argument of type 'number' is not assignable to parameter of type 'never'.
fn('foo') // Error: Argument of type 'string' is not assignable to parameter of type 'never'.
// cannot even pass `any`
declare let myAny: any
fn(myAny) // Error: Argument of type 'any' is not assignable to parameter of type 'never'.
How To Solve The Error “Argument of type not assignable to parameter type ‘never'”?
When you are having troubles with the error “Argument of type not assignable to parameter type ‘never'”, let’s try two solutions we mention below. They are very effective and helpful for you.
Solution 1: Use such a function to assure exhaustive matching within the switch and if-else expression
A function can never be invoked with any non-never value if it can only accept one never-type parameter. Using it as the default case, we ensure that all scenarios are covered because what is left must be of a type never. We can use such a function to assure exhaustive matching within the switch and if-else expression. A type error occurs if we unintentionally omit a potential match. For instance:
function unknownColor(x: never): never {
throw new Error("unknown color");
}
type Color = 'red' | 'green' | 'blue'
function getColorName(c: Color): string {
switch(c) {
case 'red':
return 'is red';
case 'green':
return 'is green';
default:
return unknownColor(c); // Error: Argument of type 'string' is not assignable to parameter of type 'never'
}
}
Solution 2: Clearly cast concat to any[]
The error also appears if we pass the empty array as our reduce call’s starting point. Concat will be termed [].concat in the initial iteration (…). Due to the strictNullChecks option, []’s inferred type is ConcatArraynever> instead of the default any[]. Therefore, a solution is to clearly cast it to any[], i.e.
const nodes = [{ name: 'a' }, { name: 'b' }];
const nodeNames = nodes.reduce( (acc, node) =>
acc.concat([node.name]),
[] as any[]
);
Conclusion
The above solutions are the best ways to solve the “Argument of type not assignable to parameter type ‘never’” error. We believe you can quickly resolve your issue with them. If you still need help or have any questions, let’s leave your comment below. We have a large community to which you can turn, and everyone is usually eager to help. Finally, we wish all of our readers a wonderful day full of new ideas. Thank you for reading!
Read more
→ TypeError: argument of type ‘int’ is not iterable – Python
Leave a comment