A
A
alpha9172018-07-05 18:05:16
typescript
alpha917, 2018-07-05 18:05:16

How to declare a function interface with multiple call options?

How to declare a function interface with multiple call options?
The function must take either one or 4 parameters
I try like this

export interface IValidator {
    (value: any): ValidationError;
    (value: any, instance: any, name: string, strict: boolean): ValidationError;
}

However, the following code throws an error when declaring BadValidator
const BadValidator: IValidator = (value: any, i: any, n: string, s: boolean): ValidationError => {
    return null;
};
const SmallValidator: IValidator = (value: any): ValidationError => {
    return null;
};

Type '(value: any, i: any, n: string, s: boolean) => ValidationError' 
is not assignable to type 'IValidator'.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question