Three questions from the bank, with answers. The practice test draws different ones each time.
Question 1 · structural typing · junior
Why is the assignment of variable `d` to `a` valid in the given TypeScript code snippet?
interface Animal {
name: string;
}
interface Dog extends Animal {
breed: string;
}
let d: Dog = { name: "Rex", breed: "Lab" };
let a: Animal = d;
A.The assignment is invalid because Animal has fewer properties than Dog (excess property check).
B.TypeScript throws a compilation error because Dog was not declared with "implements Animal".
C.TypeScript requires an explicit type assertion (as Animal) to allow this assignment.
D.The assignment is valid because TypeScript uses structural typing, and Dog has all properties required by Animal.
Show answer
D. The assignment is valid because TypeScript uses structural typing, and Dog has all properties required by Animal.
TypeScript uses structural typing (duck typing), meaning types are compatible based on their shape rather than explicit implementation. Since 'Dog' contains all properties of 'Animal' plus an additional one, a 'Dog' can be safely assigned to an 'Animal'. A common beginner trap is thinking explicit inheritance or nominal typing (like in Java) is strictly required for assignment.
Question 2 · intersections and keyof · senior
What is the type of `D` when intersecting two object types?
type A = { a: string };
type B = { b: number };
type C = A & B;
type D = keyof C;
A."a"
B."a" | "b"
C.never
D."a" & "b"
Show answer
B. "a" | "b"
An intersection type `A & B` represents an object that has all properties of both `A` and `B`. Consequently, `keyof (A & B)` evaluates to the union of their keys: `"a" | "b"`. For unions (`A | B`), `keyof` would yield the intersection of keys, but for intersections, it yields the union of keys.
Question 3 · mapped types · senior
How does this recursive mapped type `Immutable<T>` behave?
type Immutable<T> = {
readonly [K in keyof T]: T[K] extends object ? Immutable<T[K]> : T[K];
};
A.It turns all properties into optional properties instead.
B.It recursively makes all properties and nested object properties readonly.
C.It fails to compile because `readonly` cannot be used inside mapped types.
D.It only makes top-level properties readonly and ignores nested objects.
Show answer
B. It recursively makes all properties and nested object properties readonly.
Mapped types can iterate over keys using `[K in keyof T]` and apply modifiers like `readonly`. Recursively applying this creates a deep immutable type. The distractor options involve invalid mapped type syntax or failure to handle nested objects.