Skip to content

接口类型

在下方组件文档中,我们为每个组件均提供了 Types 模块,用于清晰描述该组件支持的props属性与emits事件。需要注意的是,不同组件之间可能存在共用的接口及类型声明,若在每个组件文档的 Types 模块中重复定义这些内容,不仅会造成代码冗余,还会降低整体代码的可维护性。基于此,我们将所有共用的接口与类型声明进行单独提炼、统一管理,既避免重复开发,也便于后续的修改与维护。

字典相关接口

js
/**
 * 字典接口
 */
export interface Dict {
  // 字典数据
  data?:
    | string
    | Record<string, unknown>
    | string[]
    | number[]
    | Record<string, unknown>[]
  // 字典属性
  props?: DictProps
  // 字典数据是否统一转换为String类型
  dataToString?: boolean
  // 字典分割符 默认为逗号(,)
  separator?: string
}

/**
 * 字典属性接口
 */
export interface DictProps {
  label: string
  value: string | number
}

/**
 * 字典映射接口
 */
export interface DictMap {
  [key: string]: Array<{ label: string; value: string | number }>
}

默认类型

js
/**
 * 默认类型:提取接口的「可选属性」,约束 withDefaults 只能给可选属性设置默认值
 * 支持「直接值」或「函数返回值」两种默认值形式
 * @template T - 目标 props 接口类型
 */
export type DefaultsType<T> = {
  [K in keyof T as T[K] extends Required<T>[K] ? never : K]: T[K] | (() => T[K])
}

基于 MIT 许可发布