Skip to content

TypeScript学习:自定义守卫

例:

ts
function 函数名(形参参数类型[参数类型大多为any]): 形参 is X类型 {
  return true or false
}

vue3中的应用:

ts
function isRef(r: any): r is Ref {
  return Boolean(r && r.__v_isRef === true)
}

如何判断Promise类型

ts
function isPromise(it: unknown | any): it is Promise<any> {
  return it instanceof Promise || typeof (it as any)?.then === 'function'
}

共 20 个模块,1301 篇 Markdown 文档。