Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript Function Interface overloading

Tags:

typescript

I have the following code.

interface MySecondInterface<A>{
    type: A;
}

interface MyInterface {
    (val1:string, val2:string): MySecondInterface<string>;
    (): MySecondInterface<string>;
}

const myInterfaceFn: MyInterface = (value1: string, value2:string) => {
    return {
        type: value1 + value2
    };
}



const myInterfaceFn2: MyInterface = () => {
    return {
        type: "Text"
    };
}

You can find the code here. I am getting error

Type '(value: string) => { type: string; }' is not assignable to type 'MyInterface'.

How will I create the interface to support both method signatures?

Basically, an interface for a function that takes either 2 arguments or no arguments. How can I do that?

like image 493
Neo Avatar asked May 15 '17 08:05

Neo


People also ask

Can TypeScript interfaces have functions?

A TypeScript Interface can include method declarations using arrow functions or normal functions, it can also include properties and return types. The methods can have parameters or remain parameterless.

Why do we overload a function?

Overloaded functions enable you to supply different semantics for a function, depending on the types and number of its arguments. For example, consider a print function that takes a std::string argument. This function might perform very different tasks than a function that takes an argument of type double .

How do you create a function using function overloading in Web technology?

In the above program, when different number of arguments are passed to the same function, then based on the number and type of arguments, the arguments will be passed to the respective function. In this case, we have used three different functions (function1, function2, function3) for function Overloading.


3 Answers

What about using type instead of interface?

interface MySecondInterface<A>{
  type: A;
}

type MyInterface = ((val1: string, val2: string) => MySecondInterface<string>) | (() => MySecondInterface<string>);

const myInterfaceFn: MyInterface = (value1: string, value2:string) => {
  return {
    type: value1 + value2
  };
}

const myInterfaceFn2: MyInterface = () => {
  return {
    type: "Text"
  };
}
like image 111
kimamula Avatar answered Oct 20 '22 00:10

kimamula


Your myInterfaceFn has to satisfy both function definitions in MyInterface.

The following code works fine!

interface MySecondInterface<A>{
    type: A;
}

interface MyInterface {
    (val1:string, val2:string): MySecondInterface<string>;
    (): MySecondInterface<string>;
}

const myInterfaceFn: MyInterface = (value1: string = undefined, value2:string = undefined) => {
    return {
        type: value1 !== undefined && value2 !== undefined
            ? value1 + value2
            : "Text"
    };
}



const myInterfaceFn3: MyInterface = () => {
    return {
        type: "Text"
    };
}
like image 4
Rafael Kallis Avatar answered Oct 19 '22 22:10

Rafael Kallis


Focus in on your error

Type '(value: string) => { type: string; }' is not assignable to type 'MyInterface'.

MyInterface instances must be allowed to take 0 arguments. So the following errors:

const myInterfaceFn: MyInterface = (value1: string, value2:string) => {
    return {
        type: value1 + value2
    };
}

But if you mark both as optional (e.g. using default parameters) the error goes away:

const myInterfaceFn: MyInterface = (value1 = '', value2 = '') => {
    return {
        type: value1 + value2
    };
}
like image 3
basarat Avatar answered Oct 19 '22 22:10

basarat