我在TypeScript中有两种类型,例如:
type Function1 = (text: string) => number
type Function2 = (count: number) => string
接下来,我有一些功能:
const myFunction = (text: string) => 7 // type Function1
如何找出函数的类型(Function1
或Function2
)?
if (myFunction instanceof Function1) // Not working.
if ('property' in myFunction) // ...?
这是不可能的。类型在编译期间被擦除;您不能检查运行时值并确定对其进行注释的类型。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句