Sub topic 6
Fixing Type Mismatches in Assignments and Calls
Assume you know JavaScript functions, objects, and arrays. You should also be familiar with TypeScript annotations, interfaces, and union types. This chapter focuses on two common diagnostics: TS2322 for assignments and TS2345 for function arguments.
Understand the mismatch
TypeScript checks whether a value is assignable to the type required by its location. An assignment is valid when the value can safely be used as the declared type.
function double(n: number): number {
return n * 2;
}
const input: string = "21";
double(input);The argument has type string, but double requires a number. The compiler reports TS2345:
Argument of type 'string' is not assignable to parameter of type 'number'.The same rule applies to assignments:
const count: number = "21";This produces TS2322 because a string cannot be assigned to a number variable.
Read the diagnostic as a direction: start with the value on the left side of “is not assignable,” then compare it with the required type on the right side.
flowchart TD
A[Value has a type] --> B{Is it assignable?}
B -->|Yes| C[Assignment or call is valid]
B -->|No| D[Read TS2322 or TS2345]
D --> E[Find the value's source]
E --> F[Fix conversion, annotation, or API type]caption: How TypeScript evaluates a value used in an assignment or call alt: A flowchart from a value type through an assignability check to either valid code or error investigation
Fix the value at the boundary
Convert external data before passing it to a function. Values from forms, URLs, environment variables, and JSON commonly begin as strings.
function double(n: number): number {
return n * 2;
}
const input: string = "21";
const parsed = Number(input);
if (Number.isNaN(parsed)) {
throw new Error(`Not a number: ${input}`);
}
console.log(double(parsed));The check matters because Number("hello") returns NaN, which has type number but does not represent a usable numeric value.
Do not solve this error with an assertion such as input as number. An assertion changes what the compiler believes without changing the runtime value. The function would still receive a string.
Watch for widened object properties
Literal values can widen when stored in an unannotated variable. A property initialized with "fast" may receive the broader type string, which is not assignable to the union "fast" | "safe".
type Config = {
retries: number;
mode: "fast" | "safe";
};
function run(config: Config): void {
console.log(config.retries, config.mode);
}
const config: Config = { retries: 3, mode: "fast" };
run(config);Annotating the variable records the intended shape and preserves the allowed literal union. You can also use satisfies Config when you want validation while retaining the expression’s inferred details.
Choose the right fix
| Cause | Useful fix |
|---|---|
| A string represents a number | Parse and validate it |
| The annotation is wrong | Change the annotation |
| The value may have several forms | Narrow it before use |
| The API type is too restrictive | Reconsider the API contract |
| The error is intentional in a test | Use `@ts-expect-error` |
Exercise
Why is input as number unsafe when input is a string containing "21"?
Show answer
An assertion changes only the compile-time type. It does not convert the runtime string into a number, so numeric code still receives the wrong value.