我在 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) // ...?
最佳答案
这是不可能的。类型在编译期间被删除;您无法检查运行时值并确定它所注释的类型。
关于typescript - 找出 TypeScript 中函数的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54715866/