首页 > 解决方案 > 如何将 Typescript 函数签名分配给命名函数?

问题描述

使用 typescript 时,我倾向于以这种模式编写函数:

const myFunc: MyFuncType = (input, otherInput, moreInput) => {
  return input + otherInput + moreInput;
};

但是,我似乎无法弄清楚如何将接口传递给命名函数。

我想做这样的事情:

function myFunc<MyFuncType>(input, otherInput, moreInput) {
  return input + otherInput + moreInput;
}

在第二个示例中,Typescript 分配any给我的所有函数参数,而不是推断每个参数的类型。模式是const分配函数签名的唯一方法吗?

谢谢。

标签: javascripttypescriptfunctionconventions

解决方案


推荐阅读