组件类型

Component

组件类型定义。

ts
type Component<P extends AnyProps = any> = {
  (props: P, location?: CodeLocation): RenderChild
  defaultProps?: AnyProps
  validateProps?: ValidateProps
  displayName?: string
}

说明:组件是一个接收 props 并返回视图的函数。

参考组件基础

ComponentProps

从组件类型中推断属性类型。

ts
type ComponentProps<T> = T extends Component<infer P> ? P : never

InferProps

从组件类型推断属性。

ts
type InferProps<T> = T extends Component<infer P> ? P : never

ComponentPublicInstance

组件实例公共接口。

ts
interface ComponentPublicInstance {
  readonly props: Record<string, any>
  readonly isMounted: boolean
  dispose(): void
}

属性

属性类型说明
propsRecord<string, any>组件属性
isMountedboolean是否已挂载

方法

方法说明
dispose()销毁组件

ModelRef

双向绑定模型引用类型。

ts
interface ModelRef<T extends AnyProps, K extends keyof T> {
  value: T[K]
  [key: string]: any
}

说明useModel() 返回的类型,包含 value 和事件属性,可通过扩展属性语法 ({...model}) 绑定到表单元素。

参考useModel()

Directive

指令类型。

ts
interface Directive {
  name?: string
  created?: DirectiveHook
  mounted?: DirectiveHook
  dispose?: DirectiveHook
  getSSRProps?(binding: DirectiveBinding, view: ElementView): Record<string, any> | void
}

钩子

钩子触发时机
created元素创建后
mounted元素挂载到 DOM 后
dispose元素销毁时
getSSRPropsSSR 时获取属性

DirectiveBinding

指令绑定值类型。

ts
interface DirectiveBinding {
  readonly value: any
  readonly arg?: string
}

属性

属性类型说明
valueany指令绑定的值
argstring指令参数(如 v-focus:arg 中的 arg

ViewBuilder

视图构建器类型。

ts
type ViewBuilder<P extends AnyProps = AnyProps, R extends View = View> = {
  (props: P, location?: CodeLocation): R
  [IS_VIEW_BUILDER]: true
}

说明builder() 创建的纯视图构建器类型,不具有独立的生命周期。

ValidateProps

属性验证函数类型。

ts
type ValidateProps = (props: AnyProps, location?: CodeLocation) => string | false | unknown

返回值:返回 string 表示错误消息,false 表示验证失败,其他值表示通过。

MaybeRef

可能是 ref 的类型。

ts
type MaybeRef<T> = T | Ref<T>