Question presented to candidate:
"A store exposes readonly(state) to components. Explain what readonly actually does: what happens when a component writes to it, does it still update when the store changes, and how is it different from Object.freeze?"
What a strong answer should cover:
readonly()returns a Proxy over the same data that rejectssetanddelete; it is deep, so nested objects and arrays are read-only too.- It is still reactive for reads: effects reading the view re-run when the owner mutates the original.
- In development a blocked write prints
Set operation on key "x" failed: target is readonly.; in production the write is silently ignored and does not throw, even in strict mode. shallowReadonlyblocks only top-level writes; component props are shallow read-only.readonlyis notObject.freeze: freeze changes the object itself and stops everyone, readonly is a view that only stops the holder of the view.- Related checks:
isReadonly,isReactive,isProxy,toRaw.
Clarifying questions expected:
- "Is the goal to protect state from components, or to make data truly immutable?"
- "Do you care about development warnings only, or runtime guarantees in production?"
Code / implementation expected: Optional β provide a readonly view to a child and show a blocked write plus an update through the owner.