Cannot invoke an expression whose type lacks a call signature. Type has no compatible call signatures 2349

Viewed 4244

Trying to call a getter and a setter in my service and getting the error: Cannot invoke an expression whose type lacks a call signature. Type 'Boolean' has no compatible call signatures 2349

toggleNav(): void {
    this.sidebarService.sidebarStatus.pipe(
        concatMap((bool: boolean) => this.sidebarService.updateStatus(bool))
    );
}

After reading several SO pages about this I have yet to read a simple answer. Can someone explain what this error means in plain Engrish?

Here are the setters and getters:

private _sidebarStatus = new BehaviorSubject<boolean>(true);

public get sidebarStatus(): Observable<boolean> {
    return this._sidebarStatus.asObservable();
}
public set updateStatus(bool: boolean) {
    this._sidebarStatus.next(bool);
}
1 Answers

If you are going to use properties (as you define with get/set) then you just need to assign the property when you want to set it, the call to the setter will happen automatically.

this.sidebarService.sidebarStatus.pipe(
    concatMap((bool: boolean) => this.sidebarService.updateStatus = bool)
);

The error you are getting is because the compiler treats this.sidebarService.updateStatus as a boolean field (as it should, the get/set is an implementation detail, properties behave in the same was syntactically as any field would) and thus you can't call a boolean value.

You can also drop the set and then you can call updateStatus as you try to do as it would just be a regular method not a property setter :

public updateStatus(bool: boolean) {
    this._sidebarStatus.next(bool);
}
this.sidebarService.sidebarStatus.pipe(
    concatMap((bool: boolean) => this.sidebarService.updateStatus(bool)) // ok now
);

You get read more about property accessors here under the heading Accessors

Related