0
0
Typescriptprogramming~5 mins

Fresh object literals vs variable assignment behavior in Typescript - Quick Revision & Key Differences

Choose your learning style9 modes available
Recall & Review
beginner
What is a fresh object literal in TypeScript?
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
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
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
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
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
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.