组件类型
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 : neverInferProps
从组件类型推断属性。
ts
type InferProps<T> = T extends Component<infer P> ? P : neverComponentPublicInstance
组件实例公共接口。
ts
interface ComponentPublicInstance {
readonly props: Record<string, any>
readonly isMounted: boolean
dispose(): void
}属性:
| 属性 | 类型 | 说明 |
|---|---|---|
props | Record<string, any> | 组件属性 |
isMounted | boolean | 是否已挂载 |
方法:
| 方法 | 说明 |
|---|---|
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 | 元素销毁时 |
getSSRProps | SSR 时获取属性 |
DirectiveBinding
指令绑定值类型。
ts
interface DirectiveBinding {
readonly value: any
readonly arg?: string
}属性:
| 属性 | 类型 | 说明 |
|---|---|---|
value | any | 指令绑定的值 |
arg | string | 指令参数(如 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>