Angular + TypeScript Guide
Angular + TypeScript Guide
Section titled “Angular + TypeScript Guide”Typing Angular Models
Section titled “Typing Angular Models”// User model with strict typingexport interface User { id: number; firstName: string; lastName: string; email: string; role: 'admin' | 'editor' | 'viewer'; createdAt: Date; updatedAt: Date; isActive: boolean;}
// API response typesexport interface ApiResponse<T> { data: T; success: boolean; message?: string; errors?: Record<string, string[]>;}
// Paginated responseexport interface PaginatedResponse<T> extends ApiResponse<T[]> { pagination: { page: number; pageSize: number; totalItems: number; totalPages: number; };}Typed Services
Section titled “Typed Services”@Injectable({ providedIn: 'root' })export class UserService { constructor(private http: HttpClient) {}
getUsers(): Observable<User[]> { return this.http.get<User[]>('/api/users'); }
getUser(id: number): Observable<User> { return this.http.get<User>(`/api/users/${id}`); }
createUser(user: Omit<User, 'id' | 'createdAt' | 'updatedAt'>): Observable<User> { return this.http.post<User>('/api/users', user); }
updateUser(id: number, changes: Partial<User>): Observable<User> { return this.http.patch<User>(`/api/users/${id}`, changes); }}Typed Components
Section titled “Typed Components”@Component({ selector: 'app-user-list', template: `...`,})export class UserListComponent implements OnInit { users$: Observable<User[]> = this.userService.getUsers(); selectedUser: User | null = null;
constructor(private userService: UserService) {}
ngOnInit(): void { this.loadUsers(); }
private loadUsers(): void { this.users$ = this.userService.getUsers(); }
selectUser(user: User): void { this.selectedUser = user; }}Interview Questions
Section titled “Interview Questions”Easy: How do you define models in Angular with TypeScript?
Medium: How do you type HTTP requests in Angular services?
Hard: How would you create a generic Angular service for CRUD operations?