Observable forkjoin subscribe types

Viewed 14672

I'm making 2 API requests calls with typed responses, combining them into a single Observable.forkJoin. I want to get both results in different typed variables.

var observableOrganization: Observable<Organization> = this.getOrganizationDetails();
var observablePromotion: Observable<Promotion[]> = this.getPromotions();

Observable.forkJoin([ observableOrganization, observablePromotion])
    .subscribe(
        response => {
            organization: Organization = response[0];
            promotions: Promotion[] = response[1];
        },
        error => {
            // ...
        }
    );

How can I reach typed results in single subscribe response?

2 Answers

forkJoin will only return "nice" types, when the requests are explicitly declared as ObservalbleInput.

I personally prefer this syntax, using the destructing language feature:

const requests: [ObservableInput<Organization>, ObservableInput<Promotion[]>] = [observableOrganization, observablePromotion];
Observable.forkJoin(requests)
    .subscribe(
        ([organization, promotions]: [Organization, Promotion[]]) => {
            // use organization and promotions here
        },
        error => {
            // ...
        }
    );
Related