Let's say I have the following interface:
interface MyFunctionType {
(text: string): string;
};
And the following function:
function myFunction(text) {
const newText = "new" + text;
return newText;
}
How do I define myFunction as being MyFunctionType?
I have previously been using arrow functions to get over this hurdle, such as:
const myFunction: MyFunctionType = (text) => {
const newText = "new" + text;
return newText;
}
Which works fine, however I'd prefer to use normal functions instead of arrow functions for clarity. I'd prefer not to inline the types, such as:
function myFunction(text: string): string {
const newText = "new" + text;
return newText;
}
How can I do this?
I've tried the following which doesn't work:
function myFunction(text): MyFunctionType {
const newText = "new" + text;
return newText;
}
function myFunction<MyFunctionType>(text) {
const newText = "new" + text;
return newText;
}