Sub topic 4
The Idea Behind Most Errors: Assignability
Assume you know basic JavaScript and TypeScript, including functions, objects, interfaces, and union types. This chapter focuses on the rule behind many compiler errors: assignability.
Assignability asks whether a value can safely be used where a particular type is required. The value has a source type. The place receiving it has a target type.
function double(n: number): number {
return n * 2;
}
const input: string = "21";
double(input);The argument input has source type string. The parameter n has target type number. A string cannot safely be multiplied as a number, so TypeScript reports TS2345:
Argument of type 'string' is not assignable to parameter of type 'number'.Read the error as a type relationship
Many diagnostics use one of these forms:
| Error | Common situation | Meaning |
|---|---|---|
| TS2322 | Assignment | The source value does not fit the declared target type |
| TS2345 | Function argument | An argument does not fit the parameter type |
| TS2741 | Object assignment | A required property is missing |
The wording tells you which side to inspect. In “Argument of type string is not assignable to parameter of type number,” start with the argument expression. Then compare its type with the parameter's type.
The fix should usually change the value at the boundary where its type becomes known:
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));Number(input) converts the string. The Number.isNaN check handles invalid input before the value reaches the function.
Object assignability is structural
TypeScript usually uses structural typing. A value is assignable when it has the required shape, regardless of the name of its type.
interface User {
id: number;
name: string;
}
const user = {
id: 1,
name: "Ada",
};
function greet(person: User): string {
return `Hello, ${person.name}`;
}
console.log(greet(user));The object is assignable to User because it has both required properties with the correct types. However, omitting name causes TS2741:
const incompleteUser: User = { id: 1 };The source object does not satisfy the target shape.
Literal types can be widened
A literal value can sometimes widen to a broader type. This matters when a target expects a narrow union:
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 config preserves "fast" as a valid member of the union. Without the annotation, TypeScript may infer mode as string, which is too broad. A general string might contain "slow", so it is not assignable to "fast" | "safe".
Exercise
Why is a value of type string not assignable to a parameter of type number, even when the string contains "21"?
Show answer
The type describes what operations are safe without runtime conversion. A string might contain non-numeric text, so it must be converted and validated before it is passed as a number.