I'm new to TypeScript.
I want to define Function Type having arguments with any Type. The function may one argument or more than two argument.
How should I write?
(args: any) => any
Writing this way above works only when one argument is passed to the function.
class NotificationCenter {
private observerList: Array<() => any>;
constructor() {
this.observerList = [];
}
addObserver(observer: () => any): void {
this.observerList.push(observer);
}
}
let notificationCenter: NotificationCenter = new NotificationCenter();
let observer1 = () => {};
let observer2 = (text: string) => {return "observer2"};
let observer3 = (id: number, data: Array<any>) => {return "observer3"};
//This works fine.
notificationCenter.addObserver(observer1);
//Error:Argument of type '(text: string) => string' is not assignable to parameter of type '() => any'.
notificationCenter.addObserver(observer2);
//Error:Argument of type '(id: number, data: any[]) => string' is not assignable to parameter of type '() => any'.
notificationCenter.addObserver(observer3);
(...params : any[]) => any. See this