import { warn } from 'vue' import { isObject } from '@vue/shared' import fromPairs from 'lodash/fromPairs' import type { ExtractPropTypes, PropType } from '@vue/runtime-core' import type { Mutable } from './types' const wrapperKey = Symbol() export type PropWrapper = { [wrapperKey]: T } export const propKey = Symbol() 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 IfUnknown = [unknown] extends [T] ? V : T export type BuildPropOption, R, V, C> = { type?: T values?: readonly V[] required?: R default?: R extends true ? never : D extends Record | Array ? () => D : (() => D) | D validator?: ((val: any) => val is C) | ((val: any) => boolean) } type _BuildPropType = | (T extends PropWrapper ? T[typeof wrapperKey] : [V] extends [never] ? ResolvePropTypeWithReadonly : never) | V | C export type BuildPropType = _BuildPropType< IfUnknown, IfUnknown, IfUnknown > type _BuildPropDefault = [T] extends [ // eslint-disable-next-line @typescript-eslint/ban-types Record | Array | Function ] ? D : D extends () => T ? ReturnType : D export type BuildPropDefault = R extends true ? { readonly default?: undefined } : { readonly default: Exclude extends never ? undefined : Exclude<_BuildPropDefault, undefined> } export type BuildPropReturn = { readonly type: PropType> readonly required: IfUnknown readonly validator: ((val: unknown) => boolean) | undefined [propKey]: true } & BuildPropDefault< BuildPropType, IfUnknown, 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' | 'large' | number> buildProp({ type: [String, Number], values: ['small', 'large'], 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 BuildPropType = never, R extends boolean = false, V = never, C = never >( option: BuildPropOption, key?: string ): BuildPropReturn { // filter native prop type and nested prop, e.g `null`, `undefined` (from `buildProps`) if (!isObject(option) || !!option[propKey]) return option as any 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) { const allowValuesText = [...new Set(allowedValues)] .map((value) => JSON.stringify(value)) .join(', ') warn( `Invalid prop: validation failed${ key ? ` for prop "${key}"` : '' }. Expected one of [${allowValuesText}], got value ${JSON.stringify( val )}.` ) } return valid } : undefined return { type: typeof type === 'object' && Object.getOwnPropertySymbols(type).includes(wrapperKey) ? type[wrapperKey] : type, required: !!required, default: defaultValue, validator: _validator, [propKey]: true, } as unknown as BuildPropReturn } type NativePropType = [ ((...args: any) => any) | { new (...args: any): any } | undefined | null ] export const buildProps = < O extends { [K in keyof O]: O[K] extends BuildPropReturn ? O[K] : [O[K]] extends NativePropType ? O[K] : O[K] extends BuildPropOption< infer T, infer D, infer R, infer V, infer C > ? D extends BuildPropType ? BuildPropOption : never : never } >( props: O ) => fromPairs( Object.entries(props).map(([key, option]) => [ key, buildProp(option as any, key), ]) ) as unknown as { [K in keyof O]: O[K] extends { [propKey]: boolean } ? O[K] : [O[K]] extends NativePropType ? O[K] : O[K] extends BuildPropOption< infer T, // eslint-disable-next-line @typescript-eslint/no-unused-vars 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', 'default', 'small'] as const