A fresh object literal is a newly created object directly written in the code, which TypeScript treats with exact property checks during type assignment.
Click to reveal answer
intermediate
How does TypeScript treat object literals differently from variables when assigning to a typed variable?
TypeScript performs strict property checks on fresh object literals, disallowing extra properties. Variables assigned to objects are treated as non-fresh, so extra properties are allowed if the target type is compatible.
Click to reveal answer
intermediate
Why does TypeScript sometimes give an error for extra properties in object literals but not for variables?
Because fresh object literals are checked strictly for extra properties to catch mistakes early, while variables are assumed to be intentionally typed and allow extra properties if compatible.
Click to reveal answer
beginner
What happens if you assign a fresh object literal with extra properties to a variable typed with fewer properties?
TypeScript will give an error about excess properties because fresh object literals are checked strictly for extra properties not in the target type.
Click to reveal answer
intermediate
How can you avoid excess property errors when assigning fresh object literals in TypeScript?
You can assign the object literal to a variable first, then assign that variable to the typed variable, or use type assertions to tell TypeScript to trust the type.
Click to reveal answer
What does TypeScript call an object created directly in the code with properties listed inside curly braces?
AClass instance
BVariable object
CFresh object literal
DFunction object
✗ Incorrect
An object created directly with curly braces and properties is called a fresh object literal in TypeScript.
Why does TypeScript give an error when assigning an object literal with extra properties to a typed variable?
ABecause variables cannot have extra properties
BBecause the object literal is missing required properties
CBecause TypeScript does not allow any object assignments
DBecause fresh object literals are checked strictly for extra properties
✗ Incorrect
TypeScript performs excess property checks on fresh object literals to catch mistakes early.
How can you assign an object with extra properties to a typed variable without error?
AAssign the object literal to a variable first, then assign that variable
BUse a class instead of an object literal
CRemove all properties from the object
DUse a function to create the object
✗ Incorrect
Assigning the object literal to a variable first makes it non-fresh, so TypeScript allows extra properties.
What is the main difference between fresh object literals and variables in TypeScript type checking?
AVariables are checked strictly; fresh object literals are not
BFresh object literals are checked strictly for extra properties; variables are not
CBoth are treated exactly the same
DVariables cannot be assigned to typed variables
✗ Incorrect
Fresh object literals get excess property checks, while variables do not.
Which of these is a valid way to silence excess property errors in TypeScript?
AUse a type assertion like 'as Type'
BAdd extra properties to the type
CUse a different programming language
DRemove the type annotation
✗ Incorrect
Using a type assertion tells TypeScript to trust the type and skip excess property checks.
Explain the difference between fresh object literals and variables in TypeScript when assigning to typed variables.
Think about how TypeScript checks extra properties differently for direct objects vs variables.
You got /4 concepts.
Describe two ways to avoid excess property errors when assigning objects in TypeScript.
Consider how TypeScript treats fresh vs non-fresh objects and how assertions affect checking.
You got /3 concepts.
Practice
(1/5)
1. In TypeScript, what happens when you assign an object variable to another variable using let b = a;?
easy
A. The original object is deleted.
B. A new copy of the object is created.
C. The object is converted to a string.
D. Both variables point to the same object in memory.
Solution
Step 1: Understand variable assignment with objects
Assigning one object variable to another copies the reference, not the object itself.
Step 2: Effect of reference copying
Both variables now point to the same object in memory, so changes via one affect the other.
Final Answer:
Both variables point to the same object in memory. -> Option D
Quick Check:
Object assignment copies reference = A [OK]
Hint: Assigning objects copies references, not objects themselves [OK]
Common Mistakes:
Thinking a new object is created on assignment
Assuming variables hold independent copies
Confusing object assignment with primitive assignment
2. Which of the following is the correct way to create a fresh object literal in TypeScript?
easy
A. let obj = obj2;
B. let obj = Object.assign({});
C. let obj = { name: 'Alice' };
D. let obj = new Object(obj2);
Solution
Step 1: Identify fresh object literal syntax
Using curly braces {} creates a new, fresh object literal.
Step 2: Check other options
let obj = Object.assign({}); is incorrect because Object.assign({}) needs a source object to copy from; let obj = obj2; copies reference; let obj = new Object(obj2); creates an object but with a reference or unexpected behavior.
Final Answer:
let obj = { name: 'Alice' }; -> Option C
Quick Check:
Fresh object literal uses {} = A [OK]
Hint: Use curly braces {} to create fresh objects [OK]
Common Mistakes:
Using assignment to copy objects instead of fresh literals
Misusing Object.assign without source
Confusing new Object() with fresh literal
3. What will be the output of this TypeScript code?
let a = { x: 1 };
let b = a;
b.x = 5;
console.log(a.x);
medium
A. 1
B. 5
C. undefined
D. Error
Solution
Step 1: Understand reference assignment
Variable b points to the same object as a.
Step 2: Modify property via b
Changing b.x to 5 changes the shared object property.
Step 3: Log a.x
Since a and b reference the same object, a.x is now 5.
Final Answer:
5 -> Option B
Quick Check:
Shared reference means change visible = B [OK]
Hint: Changing one reference changes all pointing to same object [OK]
Common Mistakes:
Expecting original object to remain unchanged
Confusing object copying with reference assignment
Assuming primitive copy behavior for objects
4. Identify the bug in this TypeScript code snippet:
let obj1 = { val: 10 };
let obj2 = obj1;
obj2 = { val: 20 };
console.log(obj1.val);
medium
A. obj2 assignment creates a new object, obj1 remains unchanged.
B. Both obj1 and obj2 point to the new object.
C. Syntax error due to reassignment.
D. obj1.val will be 20, which is unexpected.
Solution
Step 1: Analyze initial assignment
obj2 initially points to the same object as obj1.
Step 2: Reassign obj2 to a new object
obj2 now points to a fresh object; obj1 still points to the original.
Step 3: Check console output
obj1.val remains 10 because obj1's reference did not change.
Final Answer:
obj2 assignment creates a new object, obj1 remains unchanged. -> Option A
Quick Check:
Reassignment changes reference, original unaffected = D [OK]
Hint: Reassigning variable changes reference, original object stays same [OK]
Common Mistakes:
Thinking obj1 changes when obj2 is reassigned
Confusing property change with variable reassignment
Expecting both variables to point to new object
5. You want to create two independent objects in TypeScript so that changing one does not affect the other. Which code snippet achieves this correctly?