RxJS Types in Angular with TypeScript
RxJS Types in Angular
Section titled “RxJS Types in Angular”Observable and Subject Types
Section titled “Observable and Subject Types”// Observable typed streamconst user$: Observable<User> = this.http.get<User>('/api/users/1');
// BehaviorSubject with typeprivate loadingSubject = new BehaviorSubject<boolean>(false);loading$: Observable<boolean> = this.loadingSubject.asObservable();
// Subjectprivate errorSubject = new Subject<string | null>();error$ = this.errorSubject.asObservable();Typed Operators
Section titled “Typed Operators”import { map, filter, catchError, switchMap } from 'rxjs/operators';
const userNames$: Observable<string> = this.http.get<User[]>('/api/users').pipe( map(users => users.map(u => u.name)), switchMap(names => of(names.join(', '))));Interview Questions
Section titled “Interview Questions”Easy: How do you type an Observable in Angular?
Medium: What’s the difference between Subject and BehaviorSubject types?
Hard: How would you type a custom RxJS operator?