Angular — TypeScript
Interfaces
interface User {
id: number;
name: string;
email: string;
age?: number; // Optional
}
function greet(user: User): string {
return `Hello, ${user.name}!`;
}
Types
type Status = 'active' | 'inactive' | 'pending';
type ID = string | number;
const status: Status = 'active';
Generics
class DataStore<T> {
private items: T[] = [];
add(item: T) {
this.items.push(item);
}
getAll(): T[] {
return this.items;
}
}
const userStore = new DataStore<User>();
Decorators
function Log(target: any, key: string, descriptor: PropertyDescriptor) {
const original = descriptor.value;
descriptor.value = function(...args: any[]) {
console.log(`Calling ${key}`);
return original.apply(this, args);
};
}
class Calculator {
@Log
add(a: number, b: number) {
return a + b;
}
}
Utility Types
interface User {
id: number;
name: string;
email: string;
}
Partial<User> // All optional
Required<User> // All required
Pick<User, 'id'> // Only id
Omit<User, 'id'> // Without id
Mini Practice
- Define interfaces
- Use type unions
- Create generic classes
- Use utility types
Up Next
Continue with Security — Angular security best practices.
Related Topics
Frequently Asked Questions about TypeScript
What is TypeScript in Angular?
TypeScript is a fundamental concept in Angular. This lesson explains it step by step with clear examples, making it easy for beginners to understand.
How do I learn TypeScript?
Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn TypeScript.
Why is TypeScript important in Angular?
TypeScript is essential for Angular development. Understanding this concept will help you write better code and solve real-world problems more effectively.