Sub topic 2
What assignability means: source and target
This chapter assumes you can read basic TypeScript (annotations, interfaces, type aliases). It also assumes you have seen the compiler's usual error messages. It introduces the vocabulary the rest of the tutorial uses: a source type, a target type, and a one-way question asked between them.
The question the compiler asks
Almost every type error comes from one question: can a value of this type be used where that type is expected? TypeScript answers it with the assignability relation. The type of the value is the source. The type of the place receiving it is the target.
const n: number = 42; // source: number (from 42), target: number. OK
const s: string = 42; // Error: Type 'number' is not assignable to type 'string'.The error message is a direct statement of the relation. "Type 'number' is not assignable to type 'string'" names the source first and the target second. When you read an error, identify these two types before anything else.
The relation has a direction
Assignability is not symmetric. A source can be assignable to a target while the reverse fails.
interface Animal {
name: string;
}
interface Dog extends Animal {
bark(): void;
}
declare const dog: Dog;
declare const animal: Animal;
const a: Animal = dog; // OK: every Dog has what Animal requires
const d: Dog = animal; // Error: Property 'bark' is missing in type 'Animal' but required in type 'Dog'.The first assignment works because the source offers at least everything the target demands. The second fails because the source lacks bark. Swapping the two sides changes the answer, so always check which side is which.
The relation is also reflexive (every type is assignable to itself) and, in nearly all cases, transitive. If A is assignable to B and B is assignable to C, then A is assignable to C. Section "Where it is intentionally unsound" in a later chapter covers the exceptions that affect this.
Where the check happens
The compiler does not only check lines that contain =. It applies the same relation in every place where a value flows into a typed location.
| Context | Source | Target | Typical message start |
|---|---|---|---|
| Variable initializer or assignment | the right-hand expression | the declared variable type | Type 'X' is not assignable to type 'Y' |
| Function argument | the argument expression | the parameter type | Argument of type 'X' is not assignable to parameter of type 'Y' |
| Return statement | the returned expression | the declared return type | Type 'X' is not assignable to type 'Y' |
| Object literal property | the property value | the property type in the contextual type | Type 'X' is not assignable to type 'Y' |
The following function triggers three of these contexts.
function greet(name: string): string {
return "Hello, " + name;
}
greet("Ana"); // OK: argument "Ana" -> parameter string
greet(5); // Error: Argument of type 'number' is not assignable to parameter of type 'string'.
function broken(): number {
return "oops"; // Error: Type 'string' is not assignable to type 'number'.
}
const user: { id: number } = { id: "7" }; // Error: Type 'string' is not assignable to type 'number'.In every case the question is the same, so understanding one context teaches you the others.
Which type counts as the source and target
Two details decide what the compiler actually compares.
First, the target is the declared type of the location, not the type of whatever is stored there at the moment. Second, the source is the type of the expression at that point, which can be narrower than the variable's declared type.
let value: string | number = "a";
value.toUpperCase(); // OK: after assigning "a", value is narrowed to string
value = 5; // OK: target is the declared type string | number
value.toFixed(2); // OK: value is now narrowed to numberThe assignment value = 5 succeeds because number is assignable to the declared target string | number. The narrowing afterwards changes what you may call, but it never changes the target of later assignments.
Literal expressions also have a source type that may be more precise than you expect. In const s: string = "hi", the source is the literal type "hi", which is assignable to string. Chapter-level detail on literals appears later in this tutorial.
Assignable is not the same as "is a subtype"
You will often hear "subtype" used for this idea. For ordinary object types the two agree: Dog is a subtype of Animal and is assignable to it. The compiler's assignability relation is slightly looser, because it adds a few rules that make everyday code convenient. For example, any is assignable to nearly every type, even though it is not a subtype of them.
declare const anything: any;
const text: string = anything; // OK: any is assignable to string
const count: number = anything; // OK: and to numberTreat "assignable" as the precise term. It is what the compiler actually checks, and it includes these pragmatic exceptions. Later chapters cover each exception on its own.
Reading an error from the target backward
A practical habit for diagnosing failures:
- Find the target type. This is the type the location demands, usually written by you or inferred from a signature.
- Find the source type. Hover over the expression in your editor to see it.
- Ask what the target requires that the source does not provide.
For object types, the compiler often reports step 3 for you, as in "Property 'bark' is missing in type 'Animal' but required in type 'Dog'". For nested values, the message may be followed by indented lines. Read the last line first, since it names the innermost mismatch.
Summary of the model
- Every check compares one source type with one target type.
- The check is directional: source to target.
- It runs in assignments, arguments, returns, and object properties alike.
- The target is the declared type. The source is the expression's current type.
- Assignability is slightly broader than subtyping because of rules like
any.
The next chapters explain how the compiler decides the answer, starting with structural typing.
Exercise
Given interface Animal { name: string } and interface Cat extends Animal { meow(): void }, which of these lines produces an error, and what are its source and target types? Line 1: const c: Cat = { name: "Tom", meow() {} }; const a: Animal = c; Line 2: declare const a2: Animal; const c2: Cat = a2;
Show answer
Line 1 compiles: the source Cat is assignable to the target Animal, since Cat has everything Animal requires. Line 2 errors: the source is Animal and the target is Cat, and the message is "Property 'meow' is missing in type 'Animal' but required in type 'Cat'." The relation is directional, so swapping source and target changes the result.
Exercise
In function f(x: string | number) { x = 1; x.toFixed(); }, why does x = 1 compile, and why does x.toFixed() also compile?
Show answer
The target of x = 1 is the declared type string | number, and the source number (from the literal 1) is assignable to it. After the assignment, the compiler narrows x to number at that point, so toFixed is available. Narrowing changes the type of reads, not the target of later assignments.