I'm trying to call a function expression like this:
export default interface IUserManager {
deleteUser: ((userId: string) => Promise<void>) | ((userName: string, userSurname: string, city: string) => Promise<void>
}
from a class which holds the interface instance:
import MyUserManager from "path/to/IUserManager.ts"
class CompanyProcessor {
private myUserManager: IUserManager;
public constructor() {
this.myUserManager = new MyUserManager(); // MyUserManager implements IUserManager
}
public async deleteUser(userId: string): Promise<void> {
await this.myUserManager.deleteUser(userid); // Expected 3 argument, but got 1. ts(2554)
}
}
In VSCode, hovering over the function expression in IUserManager shows the tuple of functions. Hovering over the function call in CompanyProcessor shows only the largest function in the tuple. I've also noticed that making "userSurname" and "city" optional does work in CompanyProcessor, but doing this breaks the requirement of 3 properties, making it possible to only provide the first 2. It also does not seem to use the other functions in the tuple this way.
Example hover pop-up in the original situation:
(property) IUserManager.deleteUser: (userName: string, userSurname: string, city: string) => Promise<void>
Example hover pop-up when "userSurname" and "city" are optional:
(property) IUserManager.deleteUser: (userName: string, userSurname?: string, city?: string) => Promise<void> ( 1 overload)
My TypeScript version is 4.7.4
CodePudding user response:
You shouldn't use a union of functions. Instead you can use call signatures to describe a function with multiple overloads:
deleteUser: {
(userId: string): Promise<void>;
(userName: string, userSurname: string, city: string): Promise<void>;
};