Assignment compatibility means you can put one value into another variable without errors. It helps keep your program safe and working right.
How assignment compatibility is checked in Typescript
let targetVariable: TargetType;
targetVariable = sourceValue;The sourceValue must be compatible with the TargetType for the assignment to work.
TypeScript checks the structure and types to decide if the assignment is allowed.
let num: number; num = 5; // OK num = 'hello'; // Error
interface Person { name: string; age: number; }
let p: Person;
p = { name: 'Alice', age: 30 }; // OK
p = { name: 'Bob' }; // Errorlet x: { a: number }; let y = { a: 1, b: 2 }; x = y; // OK
This program shows that a Dog can be assigned to an Animal variable because Dog has all properties Animal needs.
interface Animal {
name: string;
}
interface Dog extends Animal {
breed: string;
}
let myPet: Animal;
let dog: Dog = { name: 'Buddy', breed: 'Beagle' };
myPet = dog; // OK because Dog has all Animal properties
console.log(myPet.name);TypeScript uses "structural typing" which means it checks if the shape of the data fits, not just the name.
Extra properties in the source are usually allowed when assigned to a target with fewer properties.
Assignment compatibility helps catch mistakes early before running the program.
Assignment compatibility means one value can be stored in a variable of another type if their shapes match.
TypeScript checks properties and types to decide if assignment is allowed.
This helps keep your code safe and avoid errors.
Practice
Solution
Step 1: Understand assignment compatibility concept
TypeScript checks if the shape (properties and their types) of one type fits into another.Step 2: Compare properties and types
It does not check variable names or runtime values but the structure of types.Final Answer:
If the properties and their types match between the source and target types -> Option DQuick Check:
Shape matching = Properties and types [OK]
- Confusing variable names with type compatibility
- Thinking runtime values affect assignment compatibility
- Assuming method count matters instead of properties
{name: string} to a variable of type {name: string; age?: number} in TypeScript?Solution
Step 1: Check optional property usage
The target type has an optional propertyage?, so the source object can omit it.Step 2: Verify assignment compatibility
Assigning{name: 'Alice'}to{name: string; age?: number}is valid becauseageis optional.Final Answer:
let person: {name: string; age?: number} = {name: 'Alice'}; -> Option BQuick Check:
Optional properties can be omitted [OK]
- Assigning object missing required properties
- Confusing optional and required properties
- Assigning extra properties to narrower types
interface A { x: number; }
interface B { x: number; y: number; }
let a: A = { x: 10 };
let b: B = { x: 10, y: 20 };
// Assignment
let c: A = b;
console.log(c.x);Solution
Step 1: Understand assignment compatibility from B to A
Type B has all properties of A plus extra. Assigning B to A is allowed because A's shape is a subset of B's.Step 2: Check the value of
Sincec.xb.xis 10,c.xwill be 10 after assignment.Final Answer:
10 -> Option AQuick Check:
Subset shape assignment = Allowed and value preserved [OK]
- Expecting error when assigning wider to narrower type
- Confusing property values with property names
- Thinking extra properties cause errors
interface Person { name: string; age: number; }
let p: Person = { name: 'Bob' };Solution
Step 1: Check required properties in interface
The interface requires bothnameandageproperties.Step 2: Verify object assigned to variable
The object only hasname, missingage, causing a type error.Final Answer:
Missing property 'age' in the object assigned to 'p' -> Option AQuick Check:
All required properties must be present [OK]
- Ignoring missing required properties
- Assuming optional properties are required
- Thinking interface cannot be used as type
interface X { a: number; b: string; }
interface Y { a: number; b: string; c?: boolean; }Which assignment is valid and why?
let x: X = { a: 1, b: 'hello' };
let y: Y = { a: 1, b: 'hello', c: true };
// Assignments
x = y;
y = x;Solution
Step 1: Analyze
Y has all properties of X plus an optionalx = yassignmentc. Assigning Y to X is allowed because X's shape is a subset of Y's.Step 2: Analyze
X lacks propertyy = xassignmentcwhich Y expects optionally. Assigning X to Y is not allowed becausecmight be missing and is optional but not guaranteed.Step 3: Reconsider optional property assignment rules
Assigning X to Y is invalid because Y expectscoptionally, but the source object does not have it, and TypeScript does not allow missing optional properties when assigning to a type that includes them.Final Answer:
Onlyx = yis valid because Y has all properties of X plus optional ones -> Option CQuick Check:
Optional properties allow assignment from wider to narrower types only [OK]
- Thinking optional properties block assignment both ways
- Assuming extra properties cause errors always
- Confusing required and optional properties
Ready to prove it?
Pass the test and this pattern moves unknown
