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?
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.
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 .
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.
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"
};
}
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"
};
}
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
};
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With