Introduction to TypeScript
Understanding what TypeScript adds to JavaScript
Introduction
In this lesson, you'll learn about introduction to typescript in TypeScript. Coming from JavaScript, you already have a foundation for understanding this concept. We'll build on that knowledge while highlighting the key differences.
In JavaScript, you're familiar with understanding what typescript adds to javascript.
TypeScript has its own approach to understanding what typescript adds to javascript, which we'll explore step by step.
The TypeScript Way
Let's see how TypeScript handles this concept. Here's a typical example:
// TypeScript - explicit types
let message: string = "Hello";
let count: number = 42;
let isActive: boolean = true;
function greet(name: string): string {
return "Hello, " + name;
}Comparing to JavaScript
Here's how you might have written similar code in JavaScript:
// JavaScript - no type information
let message = "Hello";
let count = 42;
let isActive = true;
function greet(name) {
return "Hello, " + name;
}You may be used to different syntax or behavior.
TypeScript adds optional static typing
You may be used to different syntax or behavior.
Types are checked at compile time, not runtime
You may be used to different syntax or behavior.
TypeScript compiles to plain JavaScript
Step-by-Step Breakdown
1. What is TypeScript?
TypeScript is a typed superset of JavaScript that compiles to plain JavaScript. It was developed by Microsoft and adds optional static typing, classes, and interfaces to JavaScript.
2. Type Annotations
In TypeScript, you can add type annotations using a colon (:) followed by the type name.
let name = "Alice"; // JS infers stringlet name: string = "Alice"; // TS explicit3. Why Use TypeScript?
TypeScript catches errors at compile time instead of runtime, provides better IDE support with autocompletion, and makes code self-documenting.
Common Mistakes
When coming from JavaScript, developers often make these mistakes:
- TypeScript adds optional static typing
- Types are checked at compile time, not runtime
- TypeScript compiles to plain JavaScript
Key Takeaways
- TypeScript = JavaScript + Types
- Types are optional but recommended
- Compile-time type checking prevents runtime errors