Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generic type Subject<T> requires 1 type argument(s). - Angular

I'm getting data from my service and I've red about it's important to unsubscribe after subscribe, and here is how I did it:

export class RItemComponent implements OnInit {

    apiPath = environment.apiUrl;
    private ngUnsubscribe: Subject = new Subject();

    constructor(private _sharedService: SharedService) { }

    rItems: Product[];

    ngOnInit() {
    this._sharedService.
        getReceiptItem().takeUntil(this.ngUnsubscribe).
        subscribe(products => this.rItems = products);
    }

    ngOnDestroy() {
    this.ngUnsubscribe.next();
    this.ngUnsubscribe.complete();
    }
}

But now I'm getting an error:

Generic type Subject<T> requires 1 type argument(s). subscribe

I don't understand why?

Any kind of help would be awesome, thanks!

like image 843
Roxy'Pro Avatar asked Jun 29 '18 07:06

Roxy'Pro


2 Answers

The Subject class has a generic type parameter in TypeScript. This means, that instances of this class can only be created by passing an additional type parameter, like this:

private ngUnsubscribe: Subject<MyClass> = new Subject();
like image 154
ggradnig Avatar answered Sep 28 '22 15:09

ggradnig


Add a generic type for subject

private ngUnsubscribe: Subject<any> = new Subject();
like image 31
Sachila Ranawaka Avatar answered Sep 28 '22 14:09

Sachila Ranawaka