This TypeScript error occurs when you pass an argument to a function whose type does not match the function parameter's expected type. It's a strict type checking error that prevents incompatible values from being passed to functions. Understanding type compatibility and using proper type annotations resolves this common issue.
This error indicates a type mismatch between the argument you are passing to a function and the type of the parameter that function expects. TypeScript's type checker has determined that the type of your argument (X) is not compatible with the declared parameter type (Y). This is a fundamental type safety check that prevents passing invalid values to functions. The error can occur in various scenarios: passing a string where a number is expected, passing a value that might be null where null is not allowed, passing an object missing required properties, or passing a value of a completely different type. The strict type checking catches these errors at compile time rather than allowing them to cause runtime failures.
The error message shows both the actual type (X) and the expected type (Y). Pay close attention to these types and understand what the difference is. For example, if it says "Argument of type 'string' is not assignable to parameter of type 'number'", you're passing a string where a number is expected.
// Error: Argument of type 'string' is not assignable to parameter of type 'number'
function add(a: number, b: number): number {
return a + b;
}
add("5", 10); // WRONG - first argument is string, not numberLook at the function definition and verify what type it expects. Then look at your function call and identify what type you're actually passing. In many cases, you simply need to convert the value to the correct type or use a different variable.
// CORRECT - convert string to number
const numberValue = parseInt("5", 10);
add(numberValue, 10); // number + number = OK
// OR use number literal
add(5, 10); // number + number = OKIf passing an object, verify that it has all required properties with the correct types. The error often occurs when an object is missing a required property or has a property with a different type than expected.
interface User {
id: number;
name: string;
email: string;
}
function processUser(user: User): void {
console.log(user.name);
}
// ERROR - missing required properties
processUser({ name: "John" });
// CORRECT - includes all required properties
processUser({ id: 1, name: "John", email: "[email protected]" });If the error mentions "null" or "undefined", the function parameter may not accept these values. Use type guards or provide default values to ensure you're not passing null/undefined where not allowed.
function greet(name: string): void {
console.log(`Hello, ${name}!`);
}
const userName: string | null = getUserName(); // might be null
// ERROR - might pass null
greet(userName);
// CORRECT - check for null first
if (userName !== null) {
greet(userName);
}
// OR use default value
greet(userName ?? "Guest");If you are absolutely certain the value is the correct type but TypeScript cannot infer it, you can use a type assertion with as. However, use this sparingly as it bypasses type safety.
const value: unknown = "hello";
// TypeScript doesn't know this is a string
// greet(value); // ERROR
// If you're certain it's a string, use 'as'
greet(value as string); // OKFor generic functions, make sure type parameters are correctly inferred or explicitly specified. Sometimes TypeScript cannot infer the correct type, so you need to specify it explicitly.
function identity<T>(value: T): T {
return value;
}
// TypeScript might infer the wrong type
const result1 = identity({ name: "John" }); // inferred as { name: string }
// Explicitly specify the type to be sure
interface User {
name: string;
age: number;
}
const result2 = identity<User>({ name: "John", age: 30 }); // must match User fullyIf the error involves null or undefined, check your TypeScript configuration. The strictNullChecks compiler option (enabled by "strict": true) makes null and undefined stricter. Understanding this setting helps you know when to provide additional type guards.
{
"compilerOptions": {
"strict": true, // Enables strictNullChecks
"strictNullChecks": true // null/undefined not allowed unless explicitly in type
}
}With strict mode, you must explicitly handle null/undefined:
function getName(user: { name?: string }): string {
return user.name; // ERROR: name might be undefined
// return user.name ?? ""; // CORRECT
}If a function has multiple overload signatures, ensure your argument matches one of the overload signatures. The error might indicate that your argument doesn't match any of the available overloads.
function process(value: string): string;
function process(value: number): number;
function process(value: string | number) {
return value;
}
// These work:
process("hello"); // matches first overload
process(42); // matches second overload
// This fails:
process(true); // ERROR - boolean doesn't match string or number overloadType compatibility in TypeScript follows specific rules. For objects, TypeScript uses structural typing, meaning an object type is compatible if it has all the required properties with compatible types (even if it has extra properties, unless in strict mode). For function parameters specifically, TypeScript enforces stricter rules with contravariance when strictFunctionTypes is enabled, meaning a function expecting a more general type can safely receive a function with a more specific parameter type, but not vice versa. When working with unions (type X | type Y), ensure you narrow the type before passing to functions that only accept one specific type. For generic types, type inference sometimes fails with complex nested types; explicitly specifying type parameters can help. The strictNullChecks option significantly affects error messages: without it, null and undefined are assignable to almost any type, but with it (part of strict mode), you must explicitly handle these values. For callback functions, ensure parameter types align with what the function expects; many errors occur when passing callbacks with incompatible signatures to functions like Array.map, Array.filter, or event listeners.
Function expression requires a return type
Function expression requires a return type
Value of type 'string | undefined' is not iterable
How to fix "Value is not iterable" in TypeScript
Type 'undefined' is not assignable to type 'string'
How to fix "Type undefined is not assignable to type string" in TypeScript
Type narrowing from typeof check produces 'never'
How to fix "Type narrowing produces never" in TypeScript
Type parameter 'T' has conflicting constraints
How to fix "Type parameter has conflicting constraints" in TypeScript