【发布时间】:2019-07-30 04:20:48
【问题描述】:
type AssertFuncSync = (...args: any[]) => boolean
type AssertFunc = (...args: any[]) => Promise<boolean>
我在上面的 typescript 中定义了两种类型。
现在,在函数demoFunc 中,我需要检查参数是AssertFuncSync 还是AssertFunc。我怎样才能实现它?
const demoFunc = (test_func: AssertFunc | AssertFuncSync): any => {
if (test_func is an AssertFunc) {
console.log("it belongs to AssertFunc")
}else{
console.log("it belongs to AssertFuncSync")
}
}
【问题讨论】:
-
TypeScript 不支持在运行时进行类型检查,你最好使用 javascript instanceOf。 Refer to this thread
标签: typescript