feat(dev): improve typings (#3457)

* refactor: improve buildProp

* feat(dev): add global components type for Volar
This commit is contained in:
三咲智子 2021-09-17 15:07:32 +08:00 committed by GitHub
parent 5e00a390b9
commit 1615b9ee67
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 114 additions and 11 deletions

View File

@ -18,11 +18,11 @@ import type { PropType } from 'vue'
values: ['small', 'medium'],
validator: (val: unknown): val is number => typeof val === 'number',
} as const)
@link for more explanation: https://github.com/element-plus/element-plus/pull/3341
@link read more: https://github.com/element-plus/element-plus/pull/3341
*/
export function buildProp<
T = any,
R extends boolean = boolean,
R extends boolean = false,
D extends T = T,
C = never
>({
@ -42,25 +42,32 @@ export function buildProp<
type?: any
validator?: ((val: any) => val is C) | ((val: any) => boolean)
} = {}) {
type DefaultType = typeof defaultValue
type HasDefaultValue = Exclude<T, D> extends never ? false : true
return {
type: type as PropType<T | C>,
required: !!required,
default: defaultValue,
required: !!required as R,
default: defaultValue as R extends true
? never
: HasDefaultValue extends true
? Exclude<DefaultType, undefined>
: undefined,
validator:
values || validator
? (val: unknown) => {
let vaild = false
let valid = false
if (values)
vaild ||= ([...values, defaultValue] as unknown[]).includes(val)
if (validator) vaild ||= validator(val)
return vaild
valid ||= ([...values, defaultValue] as unknown[]).includes(val)
if (validator) valid ||= validator(val)
return valid
}
: undefined,
} as const
}
export function keyOf<T>(arr: T) {
return Object.keys(arr) as Array<keyof T>
}
export const keyOf = <T>(arr: T) => Object.keys(arr) as Array<keyof T>
export const componentSize = ['large', 'medium', 'small', 'mini'] as const

96
typings/components.d.ts vendored Normal file
View File

@ -0,0 +1,96 @@
// GlobalComponents for Volar
declare module 'vue' {
export interface GlobalComponents {
ElAffix: typeof import('element-plus')['ElAffix']
ElAlert: typeof import('element-plus')['ElAlert']
ElAside: typeof import('element-plus')['ElAside']
ElAutocomplete: typeof import('element-plus')['ElAutocomplete']
ElAvatar: typeof import('element-plus')['ElAvatar']
ElBacktop: typeof import('element-plus')['ElBacktop']
ElBadge: typeof import('element-plus')['ElBadge']
ElBreadcrumb: typeof import('element-plus')['ElBreadcrumb']
ElBreadcrumbItem: typeof import('element-plus')['ElBreadcrumbItem']
ElButton: typeof import('element-plus')['ElButton']
ElButtonGroup: typeof import('element-plus')['ElButtonGroup']
ElCalendar: typeof import('element-plus')['ElCalendar']
ElCard: typeof import('element-plus')['ElCard']
ElCarousel: typeof import('element-plus')['ElCarousel']
ElCarouselItem: typeof import('element-plus')['ElCarouselItem']
ElCascader: typeof import('element-plus')['ElCascader']
ElCascaderPanel: typeof import('element-plus')['ElCascaderPanel']
ElCheckbox: typeof import('element-plus')['ElCheckbox']
ElCheckboxButton: typeof import('element-plus')['ElCheckboxButton']
ElCheckboxGroup: typeof import('element-plus')['ElCheckboxGroup']
ElCol: typeof import('element-plus')['ElCol']
ElCollapse: typeof import('element-plus')['ElCollapse']
ElCollapseItem: typeof import('element-plus')['ElCollapseItem']
ElCollapseTransition: typeof import('element-plus')['ElCollapseTransition']
ElColorPicker: typeof import('element-plus')['ElColorPicker']
ElContainer: typeof import('element-plus')['ElContainer']
ElConfigProvider: typeof import('element-plus')['ElConfigProvider']
ElDatePicker: typeof import('element-plus')['ElDatePicker']
ElDialog: typeof import('element-plus')['ElDialog']
ElDivider: typeof import('element-plus')['ElDivider']
ElDrawer: typeof import('element-plus')['ElDrawer']
ElDropdown: typeof import('element-plus')['ElDropdown']
ElDropdownItem: typeof import('element-plus')['ElDropdownItem']
ElDropdownMenu: typeof import('element-plus')['ElDropdownMenu']
ElEmpty: typeof import('element-plus')['ElEmpty']
ElFooter: typeof import('element-plus')['ElFooter']
ElForm: typeof import('element-plus')['ElForm']
ElFormItem: typeof import('element-plus')['ElFormItem']
ElHeader: typeof import('element-plus')['ElHeader']
ElIcon: typeof import('element-plus')['ElIcon']
ElImage: typeof import('element-plus')['ElImage']
ElImageViewer: typeof import('element-plus')['ElImageViewer']
ElInput: typeof import('element-plus')['ElInput']
ElInputNumber: typeof import('element-plus')['ElInputNumber']
ElLink: typeof import('element-plus')['ElLink']
ElMain: typeof import('element-plus')['ElMain']
ElMenu: typeof import('element-plus')['ElMenu']
ElMenuItem: typeof import('element-plus')['ElMenuItem']
ElMenuItemGroup: typeof import('element-plus')['ElMenuItemGroup']
ElOption: typeof import('element-plus')['ElOption']
ElOptionGroup: typeof import('element-plus')['ElOptionGroup']
ElPageHeader: typeof import('element-plus')['ElPageHeader']
ElPagination: typeof import('element-plus')['ElPagination']
ElPopconfirm: typeof import('element-plus')['ElPopconfirm']
ElPopper: typeof import('element-plus')['ElPopper']
ElProgress: typeof import('element-plus')['ElProgress']
ElRadio: typeof import('element-plus')['ElRadio']
ElRadioButton: typeof import('element-plus')['ElRadioButton']
ElRadioGroup: typeof import('element-plus')['ElRadioGroup']
ElRate: typeof import('element-plus')['ElRate']
ElRow: typeof import('element-plus')['ElRow']
ElScrollbar: typeof import('element-plus')['ElScrollbar']
ElSelect: typeof import('element-plus')['ElSelect']
ElSlider: typeof import('element-plus')['ElSlider']
ElStep: typeof import('element-plus')['ElStep']
ElSteps: typeof import('element-plus')['ElSteps']
ElSubMenu: typeof import('element-plus')['ElSubMenu']
ElSwitch: typeof import('element-plus')['ElSwitch']
ElTabPane: typeof import('element-plus')['ElTabPane']
ElTable: typeof import('element-plus')['ElTable']
ElTableColumn: typeof import('element-plus')['ElTableColumn']
ElTabs: typeof import('element-plus')['ElTabs']
ElTag: typeof import('element-plus')['ElTag']
ElTimePicker: typeof import('element-plus')['ElTimePicker']
ElTimeSelect: typeof import('element-plus')['ElTimeSelect']
ElTimeline: typeof import('element-plus')['ElTimeline']
ElTimelineItem: typeof import('element-plus')['ElTimelineItem']
ElTooltip: typeof import('element-plus')['ElTooltip']
ElTransfer: typeof import('element-plus')['ElTransfer']
ElTree: typeof import('element-plus')['ElTree']
ElUpload: typeof import('element-plus')['ElUpload']
ElSpace: typeof import('element-plus')['ElSpace']
ElSkeleton: typeof import('element-plus')['ElSkeleton']
ElSkeletonItem: typeof import('element-plus')['ElSkeletonItem']
ElCheckTag: typeof import('element-plus')['ElCheckTag']
ElDescriptions: typeof import('element-plus')['ElDescriptions']
ElDescriptionsItem: typeof import('element-plus')['ElDescriptionsItem']
ElResult: typeof import('element-plus')['ElResult']
ElSelectV2: typeof import('element-plus')['ElSelectV2']
}
}
export {}