import mapValues from 'lodash/mapValues' import { debugWarn } from './error' import type { ExtractPropTypes, PropType } from '@vue/runtime-core' import type { Mutable } from './types' const wrapperKey = Symbol() export type PropWrapper = { [wrapperKey]: T } type ResolveProp = ExtractPropTypes<{ key: { type: T; required: true } }>['key'] type ResolvePropType = ResolveProp extends { type: infer V } ? V : ResolveProp type ResolvePropTypeWithReadonly = Readonly extends Readonly< Array > ? ResolvePropType : ResolvePropType type BuildPropOption = { type?: T values?: readonly V[] required?: R default?: R extends true ? never : D extends Record | Array ? () => D : D validator?: ((val: any) => val is C) | ((val: any) => boolean) } type IfUnknown = [unknown] extends T ? V : T type BuildPropType = | (T extends PropWrapper ? T[typeof wrapperKey] : [V] extends [never] ? ResolvePropTypeWithReadonly : never) | V | C type BuildPropDefault = R extends true ? { readonly default?: undefined } : { readonly default: Exclude extends never ? undefined : Exclude< D extends Record | Array ? () => D : D, undefined > } type BuildPropReturn = { readonly type: PropType< BuildPropType, IfUnknown, IfUnknown> > readonly required: IfUnknown readonly validator: ((val: unknown) => boolean) | undefined } & BuildPropDefault, IfUnknown> /** * @description Build prop. It can better optimize prop types * @description 生成 prop,能更好地优化类型 * @example // limited options // the type will be PropType<'light' | 'dark'> buildProp({ type: String, values: ['light', 'dark'], } as const) * @example // limited options and other types // the type will be PropType<'small' | 'medium' | number> buildProp({ type: [String, Number], values: ['small', 'medium'], validator: (val: unknown): val is number => typeof val === 'number', } as const) @link see more: https://github.com/element-plus/element-plus/pull/3341 */ export function buildProp< T = never, D extends | (T extends PropWrapper ? T[typeof wrapperKey] : ResolvePropTypeWithReadonly) | V = never, R extends boolean = false, V = never, C = never >(option: BuildPropOption = {}) { const { values, required, default: defaultValue, type, validator } = option const _validator = values || validator ? (val: unknown) => { let valid = false let allowedValues: unknown[] = [] if (values) { allowedValues = [...values, defaultValue] valid ||= allowedValues.includes(val) } if (validator) valid ||= validator(val) if (!valid && allowedValues.length > 0) { debugWarn( `Vue warn`, `Invalid prop: Expected one of (${allowedValues.join( ', ' )}), got value ${val}` ) } return valid } : undefined return { type: (type as any)?.[wrapperKey] || type, required: !!required, default: defaultValue, validator: _validator, } as unknown as BuildPropReturn } export const buildProps = < O extends Record> >( options: O ) => mapValues(options, (option) => buildProp(option)) as unknown as { [K in keyof O]: O[K] extends BuildPropOption< infer T, infer D, infer R, infer V, infer C > ? BuildPropReturn : never } export const definePropType = (val: any) => ({ [wrapperKey]: val } as PropWrapper) export const keyOf = (arr: T) => Object.keys(arr) as Array export const mutable = >( val: T ) => val as Mutable export const componentSize = ['large', 'medium', 'small', 'mini'] as const