找出TypeScript中函数的类型

找出TypeScript中函数的类型,typescript,Typescript,我在TypeScript中有两种类型,例如: type Function1 = (text: string) => number type Function2 = (count: number) => string 接下来,我有一些功能: const myFunction = (text: string) => 7 // type Function1 如何找出函数类型(Function1或Function2) 这是不可能的。在编译过程中删除类型;您无法检查运行时值并确定其注

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

type Function1 = (text: string) => number
type Function2 = (count: number) => string
接下来,我有一些功能:

const myFunction = (text: string) => 7 // type Function1
如何找出函数类型(
Function1
Function2


这是不可能的。在编译过程中删除类型;您无法检查运行时值并确定其注释的类型。

问问自己在运行时如何在纯JavaScript中完成类似的操作,然后回想一下TypeScript编译为JavaScript时的类型系统。这个问题的答案从“你不能这样做”到“你必须给你的函数类型添加一些不同的属性才能使类似的东西工作”,甚至到“你可以在编译时伪造它,可能会有奇怪的运行时错误”
if (myFunction instanceof Function1) // Not working.
if ('property' in myFunction) // ...?