From dffb0cce521db2a436f458bd8dbd77e79668c5ca Mon Sep 17 00:00:00 2001 From: Amour1688 Date: Wed, 30 Sep 2020 18:20:08 +0800 Subject: [PATCH] chore: update types --- antdv-demo | 2 +- components/_util/props-util.ts | 21 +++++++++++---------- components/_util/type.ts | 12 ++++++++++++ components/_util/vue-types/utils.ts | 5 +++-- 4 files changed, 27 insertions(+), 13 deletions(-) diff --git a/antdv-demo b/antdv-demo index 79d49c0ff..83ab203d1 160000 --- a/antdv-demo +++ b/antdv-demo @@ -1 +1 @@ -Subproject commit 79d49c0ff31a4f505ccd5bc3ad238c08f9925212 +Subproject commit 83ab203d1ab9861132f6efd1e74015507c0e45f6 diff --git a/components/_util/props-util.ts b/components/_util/props-util.ts index 05ec55b6d..b154bf910 100644 --- a/components/_util/props-util.ts +++ b/components/_util/props-util.ts @@ -2,16 +2,17 @@ import { isVNode, Fragment, Comment, Text, h, VNode, ComponentPublicInstance, Sl import isPlainObject from 'lodash-es/isPlainObject'; import { camelize, hyphenate, isOn, resolvePropValue } from './util'; import isValid from './isValid'; +import { Data, PropOptions } from './type'; // function getType(fn) { // const match = fn && fn.toString().match(/^\s*function (\w+)/); // return match ? match[1] : ''; // } -const splitAttrs = attrs => { +const splitAttrs = (attrs: Data) => { const allAttrs = Object.keys(attrs); - const eventAttrs = {}; - const onEvents = {}; - const extraAttrs = {}; + const eventAttrs: Data = {}; + const onEvents: Data = {}; + const extraAttrs: Data = {}; for (let i = 0, l = allAttrs.length; i < l; i++) { const key = allAttrs[i]; if (isOn(key)) { @@ -23,8 +24,8 @@ const splitAttrs = attrs => { } return { onEvents, events: eventAttrs, extraAttrs }; }; -const parseStyleText = (cssText = '', camel) => { - const res = {}; +const parseStyleText = (cssText = '', camel: boolean) => { + const res: Record = {}; const listDelimiter = /;(?![^(]*\))/g; const propertyDelimiter = /:(.+)/; cssText.split(listDelimiter).forEach(function(item) { @@ -39,7 +40,7 @@ const parseStyleText = (cssText = '', camel) => { return res; }; -const hasProp = (instance: VNode, prop: string) => { +const hasProp = (instance: ComponentPublicInstance, prop: string) => { return prop in getOptionProps(instance); }; // 重构后直接使用 hasProp 替换 @@ -288,7 +289,7 @@ export function getEvents(ele = {}, on = true) { return splitAttrs(props)[on ? 'onEvents' : 'events']; } -export function getEvent(child, event) { +export function getEvent(child: VNode, event: string) { return child.props && child.props[event]; } @@ -374,10 +375,10 @@ export function filterEmpty(children: VNode[] = []) { } const initDefaultProps = ( propTypes: T, - defaultProps: { [K in Extract]?: any }, + defaultProps: { [K in Extract]?: T[K] }, ): T => { Object.keys(defaultProps).forEach((k: Extract) => { - const prop = propTypes[k] as PropOptions; + const prop = propTypes[k] as PropOptions; if (prop) { prop.default = defaultProps[k]; } else { diff --git a/components/_util/type.ts b/components/_util/type.ts index efe931251..4d981b16b 100644 --- a/components/_util/type.ts +++ b/components/_util/type.ts @@ -1,3 +1,5 @@ +import { PropType } from 'vue'; + export type Omit = Pick>; // https://stackoverflow.com/questions/46176165/ways-to-get-string-literal-type-of-array-values-without-enum-overhead export const tuple = (...args: T) => args; @@ -20,3 +22,13 @@ export type StringKeyOf = Extract; export type EventHandlers = { [K in StringKeyOf]?: E[K] extends Function ? E[K] : (payload: E[K]) => void; }; + +export type Data = Record; + +export declare type DefaultFactory = (props: Data) => T | null | undefined; +export declare interface PropOptions { + type?: PropType | true | null; + required?: boolean; + default?: D | DefaultFactory | null | undefined | object; + validator?(value: unknown): boolean; +} diff --git a/components/_util/vue-types/utils.ts b/components/_util/vue-types/utils.ts index 7e698c08d..5024356ee 100644 --- a/components/_util/vue-types/utils.ts +++ b/components/_util/vue-types/utils.ts @@ -33,7 +33,7 @@ export const noop = () => {}; * @param {object} obj - Object * @param {string} prop - Property to check */ -export const has = (obj: any, prop: any) => hasOwn.call(obj, prop); +export const has = (obj: object, prop: string) => hasOwn.call(obj, prop); /** * Determines whether the passed value is an integer. Uses `Number.isInteger` if available @@ -66,7 +66,8 @@ export const isArray = * @param {any} value - Value to check * @returns {boolean} */ -export const isFunction = (value: any) => toString.call(value) === '[object Function]'; +export const isFunction = (value: unknown): value is Function => + toString.call(value) === '[object Function]'; /** * Adds a `def` method to the object returning a new object with passed in argument as `default` property