typescript - 找出 TypeScript 中函数的类型

标签 typescript

我在 TypeScript 中有两种类型,例如:

type Function1 = (text: string) => number
type Function2 = (count: number) => string

接下来,我有一些功能:

const myFunction = (text: string) => 7 // type Function1

如何找出函数的类型(Function1Function2)?

if (myFunction instanceof Function1) // Not working.
if ('property' in myFunction) // ...?

最佳答案

这是不可能的。类型在编译期间被删除;您无法检查运行时值并确定它所注释的类型。

关于typescript - 找出 TypeScript 中函数的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54715866/

相关文章:

javascript - Sequelize 4 : Use native ES6 promise instead of Bluebird promise

javascript - typescript : trigger "organizeImports" from command line

angular - RxJS Observables,在发出 API 请求之前检查缓存

javascript - 简单js模块的定义文件

javascript - 通过将数据存储在类变量中来间接更改 UI 不起作用

visual-studio - ts 属性推送在 Angular 代码 VS 中的类型 '{}' 上不存在

html - Angular2 将来自服务器的 html 包含到一个 div 中

typescript - TypeScript 是否能够根据另一个论点塑造一个论点?

javascript - const 声明需要一个初始化值

Angular 6 - HttpClient - 捕获错误