📘
Introduction to TypeScript
Beginner
70 XP
25 min
Lesson Content
What is TypeScript?
TypeScript is a superset of JavaScript that adds static type checking. It was created by Microsoft and compiles to plain JavaScript.
Why TypeScript?
- Type Safety: Catch errors at compile time, not runtime
- Better IDE Support: Autocomplete, refactoring, and navigation
- Improved Code Quality: Self-documenting code with types
- Framework Ready: Essential for Angular, React, and Vue
Installing TypeScript
npm install -g typescript
# Or use npx
tsc --versionYour First TypeScript File
Create a file hello.ts:
function greet(name: string): string {
return `Hello, ${name}!`;
}
const message = greet("TypeScript");
console.log(message);Compiling TypeScript
tsc hello.ts
# This creates hello.jsTypeScript vs JavaScript
TypeScript adds:
- Type annotations
- Interfaces
- Classes with access modifiers
- Enums
- Generics
- And much more!
Example Code
Write a simple TypeScript function that greets a user
// Write a function that takes a name (string) and returns a greeting
function greet(name: string): string {
// Your code here
}
// Test your function
console.log(greet("TypeScript"));Expected Output:
Hello, TypeScript!
Study Tips
- •Read the theory content thoroughly before practicing
- •Review the example code to understand key concepts
- •Proceed to the Practice tab when you're ready to code