Types and Unions with Narrowing
Practice unions and type narrowing to model flexible inputs safely. Learn how `typeof`, `in`, and control flow help the compiler refine types at runtime boundaries.
TypeScript: Union Types
Use union types to allow multiple types.
Output
TypeScript: Type Narrowing
Narrow a union type with typeof and in.
Output
Keep Practicing
Use the online TypeScript compiler to run examples and test variations. Reinforce learning by building small scripts for each topic.
FAQ
Learn TypeScript by Practicing Examples
Hands-on practice is the fastest way to understand TypeScript. Each example focuses on a single concept—from types, unions, and narrowing to interfaces, classes, generics, and enums. Modify examples, add new functions, and see how type checks guide your code.
Use our tools to deepen learning: the TypeScript Compiler to run snippets, and the TypeScript Tutorial for structured lessons.
Beginner-Friendly
Start with type annotations and unions, then learn narrowing and basic interfaces.
Practical Patterns
Practice classes, generics, enums, tuples, and utility types in small snippets.
Grow Skills
Explore async/await and type guards to build robust, readable code.