Question presented to candidate:
"You have a Payment union where a card has cardNumber and a bank transfer has iban. How does if ('iban' in payment) narrow it, and what are the cases where the in operator surprises people?"
What a strong answer should cover:
"key" in objkeeps the union members that declarekey(required or optional) in the true branch and the members that do not declare it, or declare it optional, in the false branch- An optional property keeps that member in both branches, so the property can still be
undefined - Since 4.9, a key that no member declares is added to the type as
Record<"key", unknown>, which makesinuseful onunknown/objectinput - Run-time semantics:
inwalks the prototype chain ("toString" in {}istrue) and throws on primitives;Object.hasOwnchecks own properties only and does not narrow - When to prefer a discriminant property over
in, and whyinis the right tool for unions you do not control
Code / implementation expected: Yes β a function that narrows a union of object types with in, ideally including an optional-property case.