Akash Akash - 1 year ago 108
TypeScript Question

Arrow function in typescript interface with return type as void

I am trying to write arrow function in my type Script interface as below but getting error "Class 'ToastrService' incorrectly implements interface 'IToastr'."

interface IToastr{
(message:string,title:string):void;
}

@Injectable()
export class ToastrService implements IToastr {
(message:string,title:string):void {
toastr.success(message,title);
}
}


My question is: can we define a function signature in TS interface with return type as void??

I did try searching google as well but did not find any example. Thanks !!

Answer Source

Yes it is possible. Please consult the official typescript for more info.

Example 1:

interface SearchFunc {
    (source: string, subString: string): void;
}
let mySearch: SearchFunc;
mySearch = function(source: string, subString: string) {
    // do smth
}

Example 2: Solution: You should name your function

interface IToastr{
    FuncName(message:string,title:string):void;
}

class ToastrService implements IToastr {
    FuncName(message:string,title:string):void {
         // toastr.success(message,title);
    }
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download