用于Typescript中的函数声明的可重用类型注释?

时间:2019-03-18 01:23:14

标签: typescript

type Func = (foo:string) => void

// function expression
const myFunctionExpression:Func = function(foo) {
  console.log(foo)
}

在上面的Typescript代码段中,我使用类型别名来注释函数表达式中的变量。

类型别名:

type Func = (foo:string) => void

可在另一个函数表达式中重复使用,以减少重复。

我的问题是:有没有一种方法可以重用此类型别名来注释函数声明?

// function declaration
function myFunctionDeclaration(foo:string):void {
  console.log(foo)
}

在线搜索后,我似乎找不到这种语法,我想念什么?

谢谢

更新:

在撰写本文时,github上有一张票证要求此功能:Suggestion: Type annotations and interfaces for function declarations #22063(感谢@jcalz的评论)

1 个答案:

答案 0 :(得分:2)

在撰写本文时(TypeScript 3.4),还没有一种将类型应用于函数声明的方法。

相关问题