Syntax Cache
BlogMethodFeaturesHow It WorksBuild a Game
  1. Home
  2. TypeScript
  3. TypeScript Type Narrowing: Guards, Predicates & Patterns
TypeScript5 exercises

TypeScript Type Narrowing: Guards, Predicates & Patterns

Practice TypeScript type narrowing with typeof, instanceof, in operator, type predicates, and discriminated unions.

Type narrowing is how TypeScript refines a broad type into something more specific inside a code block. Every time you write an if check, a typeof guard, or a discriminated union switch, you're narrowing.

This page covers the key narrowing techniques. For the full reference with examples of each pattern, see the type narrowing cheat sheet.

Related TypeScript Topics
TypeScript Union & Intersection TypesTypeScript Interface vs Type: When to Use EachTypeScript Generics: Constraints, Defaults & Patterns

What You'll Practice: TypeScript Type Narrowing: Guards, Predicates & Patterns

Narrow with typeof, instanceof, and the in operatorWrite type guard functions with x is T return typesUse discriminated unions for exhaustive branchingApply exhaustive checking with neverAvoid narrowing pitfalls with closures and mutability

TypeScript Type Narrowing: Guards, Predicates & Patterns Sample Exercises

Example 1Difficulty: 1/5

Inside the if block, what is the type of x?

string
Example 2Difficulty: 1/5

Inside the if block, is name definitely a string or could it still be null?

string
Example 3Difficulty: 2/5

What operator checks if a property exists on an object for type narrowing?

in

+ 2 more exercises

Quick Reference
TypeScript Type Narrowing: Guards, Predicates & Patterns Cheat Sheet →

Copy-ready syntax examples for quick lookup

Start practicing TypeScript Type Narrowing: Guards, Predicates & Patterns

Free daily exercises with spaced repetition. No credit card required.

← Back to TypeScript Syntax Practice
Syntax Cache

Build syntax muscle memory with spaced repetition.

Product

  • Pricing
  • Our Method
  • Daily Practice
  • Design Patterns
  • Interview Prep

Resources

  • Blog
  • Compare
  • Cheat Sheets
  • Vibe Coding
  • Muscle Memory

Languages

  • Python
  • JavaScript
  • TypeScript
  • Rust
  • SQL
  • GDScript

Legal

  • Terms
  • Privacy
  • Contact

© 2026 Syntax Cache

Cancel anytime in 2 clicks. Keep access until the end of your billing period.

No refunds for partial billing periods.