chore: update types

pull/2930/head^2
Amour1688 2020-09-30 18:20:08 +08:00
parent 5526e19349
commit dffb0cce52
4 changed files with 27 additions and 13 deletions

@ -1 +1 @@
Subproject commit 79d49c0ff31a4f505ccd5bc3ad238c08f9925212 Subproject commit 83ab203d1ab9861132f6efd1e74015507c0e45f6

View File

@ -2,16 +2,17 @@ import { isVNode, Fragment, Comment, Text, h, VNode, ComponentPublicInstance, Sl
import isPlainObject from 'lodash-es/isPlainObject'; import isPlainObject from 'lodash-es/isPlainObject';
import { camelize, hyphenate, isOn, resolvePropValue } from './util'; import { camelize, hyphenate, isOn, resolvePropValue } from './util';
import isValid from './isValid'; import isValid from './isValid';
import { Data, PropOptions } from './type';
// function getType(fn) { // function getType(fn) {
// const match = fn && fn.toString().match(/^\s*function (\w+)/); // const match = fn && fn.toString().match(/^\s*function (\w+)/);
// return match ? match[1] : ''; // return match ? match[1] : '';
// } // }
const splitAttrs = attrs => { const splitAttrs = (attrs: Data) => {
const allAttrs = Object.keys(attrs); const allAttrs = Object.keys(attrs);
const eventAttrs = {}; const eventAttrs: Data = {};
const onEvents = {}; const onEvents: Data = {};
const extraAttrs = {}; const extraAttrs: Data = {};
for (let i = 0, l = allAttrs.length; i < l; i++) { for (let i = 0, l = allAttrs.length; i < l; i++) {
const key = allAttrs[i]; const key = allAttrs[i];
if (isOn(key)) { if (isOn(key)) {
@ -23,8 +24,8 @@ const splitAttrs = attrs => {
} }
return { onEvents, events: eventAttrs, extraAttrs }; return { onEvents, events: eventAttrs, extraAttrs };
}; };
const parseStyleText = (cssText = '', camel) => { const parseStyleText = (cssText = '', camel: boolean) => {
const res = {}; const res: Record<string, string> = {};
const listDelimiter = /;(?![^(]*\))/g; const listDelimiter = /;(?![^(]*\))/g;
const propertyDelimiter = /:(.+)/; const propertyDelimiter = /:(.+)/;
cssText.split(listDelimiter).forEach(function(item) { cssText.split(listDelimiter).forEach(function(item) {
@ -39,7 +40,7 @@ const parseStyleText = (cssText = '', camel) => {
return res; return res;
}; };
const hasProp = (instance: VNode, prop: string) => { const hasProp = (instance: ComponentPublicInstance, prop: string) => {
return prop in getOptionProps(instance); return prop in getOptionProps(instance);
}; };
// 重构后直接使用 hasProp 替换 // 重构后直接使用 hasProp 替换
@ -288,7 +289,7 @@ export function getEvents(ele = {}, on = true) {
return splitAttrs(props)[on ? 'onEvents' : 'events']; return splitAttrs(props)[on ? 'onEvents' : 'events'];
} }
export function getEvent(child, event) { export function getEvent(child: VNode, event: string) {
return child.props && child.props[event]; return child.props && child.props[event];
} }
@ -374,10 +375,10 @@ export function filterEmpty(children: VNode[] = []) {
} }
const initDefaultProps = <T>( const initDefaultProps = <T>(
propTypes: T, propTypes: T,
defaultProps: { [K in Extract<keyof T, string>]?: any }, defaultProps: { [K in Extract<keyof T, string>]?: T[K] },
): T => { ): T => {
Object.keys(defaultProps).forEach((k: Extract<keyof T, string>) => { Object.keys(defaultProps).forEach((k: Extract<keyof T, string>) => {
const prop = propTypes[k] as PropOptions<any>; const prop = propTypes[k] as PropOptions;
if (prop) { if (prop) {
prop.default = defaultProps[k]; prop.default = defaultProps[k];
} else { } else {

View File

@ -1,3 +1,5 @@
import { PropType } from 'vue';
export type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>; export type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;
// https://stackoverflow.com/questions/46176165/ways-to-get-string-literal-type-of-array-values-without-enum-overhead // https://stackoverflow.com/questions/46176165/ways-to-get-string-literal-type-of-array-values-without-enum-overhead
export const tuple = <T extends string[]>(...args: T) => args; export const tuple = <T extends string[]>(...args: T) => args;
@ -20,3 +22,13 @@ export type StringKeyOf<T> = Extract<keyof T, string>;
export type EventHandlers<E> = { export type EventHandlers<E> = {
[K in StringKeyOf<E>]?: E[K] extends Function ? E[K] : (payload: E[K]) => void; [K in StringKeyOf<E>]?: E[K] extends Function ? E[K] : (payload: E[K]) => void;
}; };
export type Data = Record<string, unknown>;
export declare type DefaultFactory<T> = (props: Data) => T | null | undefined;
export declare interface PropOptions<T = any, D = T> {
type?: PropType<T> | true | null;
required?: boolean;
default?: D | DefaultFactory<D> | null | undefined | object;
validator?(value: unknown): boolean;
}

View File

@ -33,7 +33,7 @@ export const noop = () => {};
* @param {object} obj - Object * @param {object} obj - Object
* @param {string} prop - Property to check * @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 * 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 * @param {any} value - Value to check
* @returns {boolean} * @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 * Adds a `def` method to the object returning a new object with passed in argument as `default` property