TypeScript Basics
๐ Covered in this chapter:
Interfaces ยท Type Aliases ยท Union Types ยท Generics ยท Type Narrowing
Learn the TypeScript fundamentals you need before adding types to a Node.js backend: interfaces, unions, and generics.
1TypeScript Basics โ What You'll Learn
Learn the TypeScript fundamentals you need before adding types to a Node.js backend: interfaces, unions, and generics.
Here's everything this chapter covers, in the order you'll learn it:
- What TypeScript is and why teams adopt it
- Interfaces
- Type aliases
- Union types
- Generics
- Function types
- Type narrowing
- Enums (overview)
- Optional properties
- Typing API response shapes
2Working Example
๐ป Example: TypeScript Basics
TypeScript
โถ Run in Compiler
type Course = {
id: number;
title: string;
level: string;
};
function formatCourse(course: Course): string {
return `${course.title} - ${course.level}`;
}
3Best Practices & Common Pitfalls
๐ก Key things to remember:
- TypeScript compiles down to plain JavaScript โ Node.js itself doesn't run TypeScript directly, which is why a build (or a tool like tsx) is needed.
โ Frequently Asked Questions (FAQ)
Q What's the most important thing to understand about typescript basics?
Focus on: Interfaces ยท Type Aliases ยท Union Types ยท Generics ยท Type Narrowing. These are the core building blocks this chapter's examples are built around, and they show up repeatedly in later chapters of this course.
Q Do I need external npm packages for typescript basics?
Only where explicitly shown in the code examples above (like Express, Zod, or Socket.IO) โ otherwise, this chapter relies entirely on Node.js's own built-in capabilities.