Typescript 中函数声明的可重用类型注释? [英] Reusable type annotation for function declaration in Typescript?

查看:31
本文介绍了Typescript 中函数声明的可重用类型注释?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

type Func = (foo:string) => void

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

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

In the Typescript snippet above, I am using type alias to annotate the variable in a function expression.

类型别名:

type Func = (foo:string) => void

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

is reusable in another function expression to reduce repetition.

我的问题是:有没有办法重用这个类型别名来注释函数声明?

My question is: Is there a way to reuse this type alias to annotate a function declaration ?

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

在网上搜索后,我似乎找不到这样的语法,我错过了什么?

After some search online, I cannot seem to find such syntax, what am I missing?

谢谢

更新:

在撰写本文时,github 上有一张请求此功能的票:建议:函数声明的类型注释和接口 #22063(感谢@jcalz 的评论)

At the time of this writing there is a ticket on github requesting for this feature: Suggestion: Type annotations and interfaces for function declarations #22063 (thanks to comment from @jcalz)

推荐答案

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

At time of writing (TypeScript 3.4), there is not a way to apply a type to a function declaration.

这篇关于Typescript 中函数声明的可重用类型注释?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆