2021-09-29 09:52:58 +08:00
|
|
|
|
import mapValues from 'lodash/mapValues'
|
2021-09-22 20:27:23 +08:00
|
|
|
|
import { debugWarn } from './error'
|
|
|
|
|
import type { ExtractPropTypes, PropType } from '@vue/runtime-core'
|
2021-09-22 01:19:04 +08:00
|
|
|
|
import type { Mutable } from './types'
|
2021-09-22 20:27:23 +08:00
|
|
|
|
|
|
|
|
|
const wrapperKey = Symbol()
|
|
|
|
|
export type PropWrapper<T> = { [wrapperKey]: T }
|
|
|
|
|
|
|
|
|
|
type ResolveProp<T> = ExtractPropTypes<{
|
|
|
|
|
key: { type: T; required: true }
|
|
|
|
|
}>['key']
|
|
|
|
|
type ResolvePropType<T> = ResolveProp<T> extends { type: infer V }
|
|
|
|
|
? V
|
|
|
|
|
: ResolveProp<T>
|
|
|
|
|
type ResolvePropTypeWithReadonly<T> = Readonly<T> extends Readonly<
|
|
|
|
|
Array<infer A>
|
|
|
|
|
>
|
|
|
|
|
? ResolvePropType<A[]>
|
|
|
|
|
: ResolvePropType<T>
|
2021-09-12 19:38:48 +08:00
|
|
|
|
|
2021-09-29 09:52:58 +08:00
|
|
|
|
type BuildPropOption<T, D, R, V, C> = {
|
|
|
|
|
type?: T
|
|
|
|
|
values?: readonly V[]
|
|
|
|
|
required?: R
|
|
|
|
|
default?: R extends true
|
|
|
|
|
? never
|
|
|
|
|
: D extends Record<string, unknown> | Array<any>
|
|
|
|
|
? () => D
|
|
|
|
|
: D
|
|
|
|
|
validator?: ((val: any) => val is C) | ((val: any) => boolean)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type IfUnknown<T, V> = [unknown] extends T ? V : T
|
|
|
|
|
|
|
|
|
|
type BuildPropType<T, V, C> =
|
|
|
|
|
| (T extends PropWrapper<unknown>
|
|
|
|
|
? T[typeof wrapperKey]
|
|
|
|
|
: [V] extends [never]
|
|
|
|
|
? ResolvePropTypeWithReadonly<T>
|
|
|
|
|
: never)
|
|
|
|
|
| V
|
|
|
|
|
| C
|
|
|
|
|
|
|
|
|
|
type BuildPropDefault<D, R> = R extends true
|
|
|
|
|
? { readonly default?: undefined }
|
|
|
|
|
: {
|
|
|
|
|
readonly default: Exclude<D, undefined> extends never
|
|
|
|
|
? undefined
|
|
|
|
|
: Exclude<
|
|
|
|
|
D extends Record<string, unknown> | Array<any> ? () => D : D,
|
|
|
|
|
undefined
|
|
|
|
|
>
|
|
|
|
|
}
|
|
|
|
|
type BuildPropReturn<T, D, R, V, C> = {
|
|
|
|
|
readonly type: PropType<
|
|
|
|
|
BuildPropType<IfUnknown<T, never>, IfUnknown<V, never>, IfUnknown<C, never>>
|
|
|
|
|
>
|
|
|
|
|
readonly required: IfUnknown<R, false>
|
|
|
|
|
readonly validator: ((val: unknown) => boolean) | undefined
|
|
|
|
|
} & BuildPropDefault<IfUnknown<D, never>, IfUnknown<R, false>>
|
|
|
|
|
|
2021-09-12 19:38:48 +08:00
|
|
|
|
/**
|
|
|
|
|
* @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)
|
2021-09-21 17:19:35 +08:00
|
|
|
|
@link see more: https://github.com/element-plus/element-plus/pull/3341
|
2021-09-12 19:38:48 +08:00
|
|
|
|
*/
|
|
|
|
|
export function buildProp<
|
2021-09-22 20:27:23 +08:00
|
|
|
|
T = never,
|
|
|
|
|
D extends
|
|
|
|
|
| (T extends PropWrapper<any>
|
|
|
|
|
? T[typeof wrapperKey]
|
|
|
|
|
: ResolvePropTypeWithReadonly<T>)
|
|
|
|
|
| V = never,
|
2021-09-17 15:07:32 +08:00
|
|
|
|
R extends boolean = false,
|
2021-09-22 20:27:23 +08:00
|
|
|
|
V = never,
|
2021-09-12 19:38:48 +08:00
|
|
|
|
C = never
|
2021-09-29 09:52:58 +08:00
|
|
|
|
>(option: BuildPropOption<T, D, R, V, C> = {}) {
|
|
|
|
|
const { values, required, default: defaultValue, type, validator } = option
|
|
|
|
|
|
2021-09-23 20:06:07 +08:00
|
|
|
|
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
|
|
|
|
|
|
2021-09-12 19:38:48 +08:00
|
|
|
|
return {
|
2021-09-23 20:06:07 +08:00
|
|
|
|
type: (type as any)?.[wrapperKey] || type,
|
|
|
|
|
required: !!required,
|
|
|
|
|
default: defaultValue,
|
|
|
|
|
validator: _validator,
|
2021-09-29 09:52:58 +08:00
|
|
|
|
} as unknown as BuildPropReturn<T, D, R, V, C>
|
2021-09-12 19:38:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-09-29 09:52:58 +08:00
|
|
|
|
export const buildProps = <
|
|
|
|
|
O extends Record<string, BuildPropOption<any, any, any, any, any>>
|
|
|
|
|
>(
|
|
|
|
|
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<T, D, R, V, C>
|
|
|
|
|
: never
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-22 20:27:23 +08:00
|
|
|
|
export const definePropType = <T>(val: any) =>
|
|
|
|
|
({ [wrapperKey]: val } as PropWrapper<T>)
|
|
|
|
|
|
2021-09-17 15:07:32 +08:00
|
|
|
|
export const keyOf = <T>(arr: T) => Object.keys(arr) as Array<keyof T>
|
2021-09-22 20:27:23 +08:00
|
|
|
|
export const mutable = <T extends readonly any[] | Record<string, unknown>>(
|
|
|
|
|
val: T
|
|
|
|
|
) => val as Mutable<typeof val>
|
2021-09-12 19:38:48 +08:00
|
|
|
|
|
|
|
|
|
export const componentSize = ['large', 'medium', 'small', 'mini'] as const
|