TypeScript tutorialLesson 27Free preview
TS Type Guards
This lesson is part of TypeScript · All levels
Summary
Type guard is a runtime check that allows the compiler to infer narrower types on a particular block. Operators typeof and instanceof are built-in guards, while custom guards can be written with the return type predicate pattern. This technique is crucial to working safely with the union type.
Key points
- Distinguish between primitive
- Check the instance class
- Predicate v is T tells the compiler the test results
Full lesson
Code examples and quizzes
Full lessons, code examples, and quizzes are available to subscribers. 5 questions.