📖 Scenario: Imagine you are working on a TypeScript project where you need to assign values between different types. Understanding how TypeScript checks if one type can be assigned to another helps you avoid errors and write safer code.
🎯 Goal: You will create variables with different types and check how TypeScript allows or disallows assignments based on compatibility rules.
📋 What You'll Learn
Create variables with specific types
Create a helper variable to hold a value
Assign variables to each other to test compatibility
Print the results or comments about compatibility
💡 Why This Matters
🌍 Real World
In real projects, understanding assignment compatibility helps prevent bugs and ensures your code works as expected when variables interact.
💼 Career
Many programming jobs require writing safe and maintainable TypeScript code, so knowing how assignment compatibility works is essential.
Progress0 / 4 steps
1
Create variables with specific types
Create a variable called num of type number and set it to 10. Create a variable called str of type string and set it to "hello".
Typescript
Hint
Use let keyword and specify types with : after variable names.
2
Create a helper variable to hold a value
Create a variable called value of type any and set it to num.
Typescript
Hint
The any type can hold any value without type errors.
3
Assign variables to test compatibility
Assign value to str. Then assign str to value. Use the exact code str = value; and value = str;.
Typescript
Hint
Because value is any, it can be assigned to str and vice versa.
4
Print the results
Write console.log(str); to print the value of str.
Typescript
Hint
The output will be the value assigned to str after assignment.
Practice
(1/5)
1. In TypeScript, what does assignment compatibility primarily check between two types?
easy
A. If the types have the same number of methods
B. If the variable names are the same
C. If the values are equal at runtime
D. If the properties and their types match between the source and target types
Assuming method count matters instead of properties
2. Which of the following is the correct syntax to assign an object of type {name: string} to a variable of type {name: string; age?: number} in TypeScript?
easy
A. let person: {name: string; age: number} = {age: 30};
B. let person: {name: string; age?: number} = {name: 'Alice'};
C. let person: {name: string; age: number} = {name: 'Alice'};
D. let person: {name: string} = {name: 'Alice', age: 30};
Solution
Step 1: Check optional property usage
The target type has an optional property age?, so the source object can omit it.
Step 2: Verify assignment compatibility
Assigning {name: 'Alice'} to {name: string; age?: number} is valid because age is optional.
Final Answer:
let person: {name: string; age?: number} = {name: 'Alice'}; -> Option B
Quick Check:
Optional properties can be omitted [OK]
Hint: Optional properties can be left out in assignments [OK]
Common Mistakes:
Assigning object missing required properties
Confusing optional and required properties
Assigning extra properties to narrower types
3. What will be the output of the following TypeScript code?
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);
medium
A. 10
B. 20
C. undefined
D. Compilation error
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 c.x
Since b.x is 10, c.x will be 10 after assignment.
Final Answer:
10 -> Option A
Quick Check:
Subset shape assignment = Allowed and value preserved [OK]
Hint: Assign wider type to narrower type if subset matches [OK]
Common Mistakes:
Expecting error when assigning wider to narrower type
Confusing property values with property names
Thinking extra properties cause errors
4. Identify the error in this TypeScript code snippet:
interface Person { name: string; age: number; }
let p: Person = { name: 'Bob' };
medium
A. Missing property 'age' in the object assigned to 'p'
B. Property 'name' should be a number, not a string
C. Interface 'Person' cannot be used as a type
D. No error, code is valid
Solution
Step 1: Check required properties in interface
The interface requires both name and age properties.
Step 2: Verify object assigned to variable
The object only has name, missing age, causing a type error.
Final Answer:
Missing property 'age' in the object assigned to 'p' -> Option A
Quick Check:
All required properties must be present [OK]
Hint: Check all required properties are present in assigned object [OK]
Common Mistakes:
Ignoring missing required properties
Assuming optional properties are required
Thinking interface cannot be used as type
5. Given these interfaces:
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;
hard
A. Both assignments are valid because optional properties don't matter
B. Only y = x is valid because X has fewer properties
C. Only x = y is valid because Y has all properties of X plus optional ones
D. Both assignments are invalid due to property mismatch
Solution
Step 1: Analyze x = y assignment
Y has all properties of X plus an optional c. Assigning Y to X is allowed because X's shape is a subset of Y's.
Step 2: Analyze y = x assignment
X lacks property c which Y expects optionally. Assigning X to Y is not allowed because c might be missing and is optional but not guaranteed.
Assigning X to Y is invalid because Y expects c optionally, 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:
Only x = y is valid because Y has all properties of X plus optional ones -> Option C
Quick Check:
Optional properties allow assignment from wider to narrower types only [OK]
Hint: Optional properties allow assignment from wider to narrower types only [OK]
Common Mistakes:
Thinking optional properties block assignment both ways
Assuming extra properties cause errors always
Confusing required and optional properties
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.