Argument of type is not assignable to parameter of type
The type of an argument passed to a function does not match the type of the parameter defined in the function signature.
- 1Passing a string to a function expecting a number.
- 2Providing an object that is missing a required property.
- 3Passing 'null' or 'undefined' to a function that does not explicitly allow it.
A function expecting a number is called with a string.
function printValue(value: number) {
console.log(value);
}
printValue("hello");expected output
error TS2345: Argument of type 'string' is not assignable to parameter of type 'number'.
Fix 1
Pass an argument of the correct type
WHEN The function is being called with the wrong data.
function printValue(value: number) {
console.log(value);
}
printValue(123);Why this works
Providing a value that matches the parameter's type signature.
Fix 2
Adjust the function's parameter type
WHEN The function needs to handle different types.
function printValue(value: number | string) {
console.log(value);
}
printValue("hello");Why this works
Using a union type to allow multiple types for the parameter.
✕ Use 'as any' to cast the argument
This silences the compiler but undermines type safety, potentially causing runtime errors.
microsoft/TypeScript src/compiler/diagnosticMessages.json
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev