TypeScript
TypeScript found an argument that is not assignable to the parameter type
TypeScript rejected a function call because one of the provided arguments does not match the type expected by the target parameter.
TypeScript
TypeScript rejected a function call because one of the provided arguments does not match the type expected by the target parameter.
Observed message
error TS2345: Argument of type 'X' is not assignable to parameter of type 'Y'.
Why it happens
How to fix it
function repeatCount(count: number) {
return count * 2;
}
repeatCount("3");function repeatCount(count: number) {
return count * 2;
}
repeatCount(3);The string argument produces TS2345; the corrected numeric argument compiles and returns 6. If the input is user text, validate and parse it instead of asserting that the string is a number.
function shout(value: string) { return value.toUpperCase(); }
function show(value: string | number) {
return shout(value);
}function shout(value: string) { return value.toUpperCase(); }
function show(value: string | number) {
if (typeof value === "string") return shout(value);
return String(value);
}The guard proves the string branch is safe; the numeric branch has explicit behavior. This uses ordinary control-flow narrowing supported in TypeScript 5.x and 6.x.
Official reference
TypeScript Handbook: NarrowingRelated errors
Explore more