【发布时间】:2019-10-04 11:40:39
【问题描述】:
假设我有以下界面:
interface MyFunctionType {
(text: string): string;
};
还有以下功能:
function myFunction(text) {
const newText = "new" + text;
return newText;
}
如何将myFunction 定义为MyFunctionType?
我之前一直在使用箭头函数来克服这个障碍,例如:
const myFunction: MyFunctionType = (text) => {
const newText = "new" + text;
return newText;
}
这很好用,但是为了清楚起见,我更喜欢使用普通函数而不是箭头函数。我不想内联类型,例如:
function myFunction(text: string): string {
const newText = "new" + text;
return newText;
}
我该怎么做?
我尝试了以下不起作用:
function myFunction(text): MyFunctionType {
const newText = "new" + text;
return newText;
}
function myFunction<MyFunctionType>(text) {
const newText = "new" + text;
return newText;
}
【问题讨论】:
标签: typescript