0
0
Typescriptprogramming~5 mins

How assignment compatibility is checked in Typescript - Quick Revision & Summary

Choose your learning style9 modes available
Recall & Review
beginner
What does assignment compatibility mean in TypeScript?
It means you can assign a value of one type to a variable of another type if TypeScript considers the types compatible based on their structure.
Click to reveal answer
intermediate
How does TypeScript check assignment compatibility between two object types?
TypeScript checks if the source object has at least the properties and types required by the target object (structural typing). Extra properties in the source are allowed.
Click to reveal answer
beginner
What happens if you try to assign a value with missing required properties in TypeScript?
TypeScript will give an error because the source type does not have all the required properties of the target type, so they are not assignment compatible.
Click to reveal answer
intermediate
Can TypeScript assign a function type to another function type with fewer parameters?
Yes, TypeScript allows assigning a function with fewer parameters to one expecting more, as long as the parameters it does have are compatible (called function parameter bivariance).
Click to reveal answer
advanced
Does TypeScript consider private or protected members when checking assignment compatibility?
Yes, private and protected members must come from the same declaration for assignment compatibility; otherwise, the types are incompatible.
Click to reveal answer
In TypeScript, when is an object type assignment allowed?
AWhen the target has more properties than the source
BOnly when both objects have exactly the same properties
CWhen the source has fewer properties than the target
DWhen the source has all properties required by the target, plus possibly more
What happens if you assign a function with more parameters to a function type expecting fewer parameters?
AIt's allowed because extra parameters are ignored
BIt's not allowed because of parameter mismatch
CIt's allowed only if parameters have the same names
DIt's allowed only if return types differ
Are private members considered when checking assignment compatibility?
AYes, they must come from the same declaration
BNo, private members are ignored
COnly if the classes are unrelated
DOnly if the members are static
Which typing system does TypeScript use to check assignment compatibility?
AStructural typing
BNominal typing
CDuck typing
DDynamic typing
Can you assign a value of type 'number' to a variable of type 'string' in TypeScript?
AYes, if you use type assertion
BYes, always
CNo, because number and string are incompatible
DOnly if the variable is declared with 'any'
Explain how TypeScript checks assignment compatibility between two object types.
Think about the shape of the objects and member visibility.
You got /4 concepts.
    Describe how function types are checked for assignment compatibility in TypeScript.
    Consider how many parameters the functions have and their types.
    You got /4 concepts.