Structural typing means that two types are compatible if their shapes (properties and methods) match, regardless of their names.
Click to reveal answer
beginner
What is nominal typing?
Nominal typing means that two types are compatible only if they have the same name or explicitly declared relationship, regardless of their structure.
Click to reveal answer
beginner
How does TypeScript use typing?
TypeScript uses structural typing, so it checks if types have matching properties and methods rather than relying on type names.
Click to reveal answer
intermediate
Example: Why are two objects with the same properties compatible in structural typing?
Because structural typing compares the shape (properties and methods), two objects with the same shape are considered the same type even if their names differ.
Click to reveal answer
intermediate
Why might nominal typing be preferred in some languages?
Nominal typing helps catch errors by requiring explicit type relationships, making sure only intended types are compatible, which can improve safety.
Click to reveal answer
In structural typing, two types are compatible if:
AThey have the same shape (properties and methods).
BThey have the same name.
CThey are declared in the same file.
DThey have different properties.
✗ Incorrect
Structural typing checks if the shapes match, not the names.
Which typing system requires explicit type names to match for compatibility?
AStructural typing
BDynamic typing
CDuck typing
DNominal typing
✗ Incorrect
Nominal typing requires type names or explicit declarations to match.
TypeScript primarily uses which typing system?
ANominal typing
BDynamic typing
CStructural typing
DDuck typing
✗ Incorrect
TypeScript uses structural typing to check type compatibility.
If two objects have the same properties but different names, are they compatible in TypeScript?
ANo, because names must match.
BYes, because TypeScript uses structural typing.
COnly if explicitly declared.
DOnly if they are classes.
✗ Incorrect
TypeScript compares object shapes, so names don't matter.
Which typing system can help prevent accidental type compatibility?
ANominal typing
BStructural typing
CDuck typing
DDynamic typing
✗ Incorrect
Nominal typing requires explicit type matches, reducing accidental compatibility.
Explain in your own words how structural typing differs from nominal typing.
Think about whether type names or properties matter more.
You got /3 concepts.
Why might a language choose nominal typing over structural typing?
Consider how strict the type system should be.
You got /3 concepts.
Practice
(1/5)
1. Which of the following best describes structural typing in TypeScript?
easy
A. Types are compatible if their properties and types match, regardless of their names.
B. Types must have the exact same name to be compatible.
C. Types are only compatible if they come from the same class.
D. Types are checked only at runtime for compatibility.
Solution
Step 1: Understand structural typing concept
Structural typing means compatibility depends on the shape (properties and types) of objects, not their names.
Step 2: Compare with nominal typing
Nominal typing requires exact type names or declarations to match, which is not how TypeScript works by default.
Final Answer:
Types are compatible if their properties and types match, regardless of their names. -> Option A
B. class A {} class B extends A {} const a: A = new B();
C. type A = {id: number}; type B = {name: string}; const a: A = {id: 1}; const b: B = a;
D. interface A {name: string;} interface B {name: number;} const a: A = {name: 'Bob'}; const b: B = a;
Solution
Step 1: Check property shapes for compatibility
const a: {name: string} = {name: 'Alice'}; const b: {name: string} = a; assigns an object with a matching property name: string to another variable with the same shape, which is allowed.
Step 2: Analyze other options for incompatibility
A mismatches properties (id vs name), B uses classes (nominal typing), D mismatches property types.