QuizNot attempted
Type guards
1–4 to choose, Enter to submit
Show answer & explanation
Correct answer:
- When it returns true, TS narrows x to User at the call site
- A type predicate
x is Usertells the compiler to narrowxtoUserin the true branch at the call site - a plain
booleanreturn gives no narrowing. - Not a runtime check: TS trusts your implementation and emits nothing, so a wrong guard silently lies.
- Not throws: it returns true/false
- throwing is an assertion function (
asserts x is User). - Not 'nothing': the narrowing is the whole point.
- Validate untrusted data with a schema (zod).