Majeed Siddiqui Majeed Siddiqui - 3 months ago 10
TypeScript Question

What does interface with two functions type declarations means and how to use it?

interface MyInterface {
(select: string): number;
(element: number): string;
}


How do I use above interface? What is the use case?

Answer

It means that the function has two different signatures, or in other wise overloading.
For example:

interface MyInterface {
    (select: string): number;
    (element: number): string;
}

let fn: MyInterface;
fn = (obj): any => {
    // do something 
    return null;
}

let str = fn(3); // type of str is string
let num = fn("string"); // typeof num is number
let error = fn(false); // error as false is neither string or number

(code in playground)