From 74f1723e428130988dd1ff9c98ef698aca034cf1 Mon Sep 17 00:00:00 2001 From: iapyang Date: Wed, 4 Sep 2019 11:09:38 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20isXXX=E7=9A=84=E6=96=B9=E6=B3=95?= =?UTF-8?q?=E8=BF=94=E5=9B=9E=E5=80=BC=E4=BD=BF=E7=94=A8type=20guard?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- typescript/core/base.ts | 46 +++++++++++++++++++++-------------------- 1 file changed, 24 insertions(+), 22 deletions(-) diff --git a/typescript/core/base.ts b/typescript/core/base.ts index dc87b71da4..32c83c3a7d 100644 --- a/typescript/core/base.ts +++ b/typescript/core/base.ts @@ -1,3 +1,5 @@ +import { _Widget } from "./widget"; + export interface _base { assert: (v: any, is: Function) => Boolean @@ -5,7 +7,7 @@ export interface _base { UUID: () => string; - isWidget: (widget: any) => Boolean; + isWidget: (widget: any) => widget is _Widget; createWidgets: (items: any, options: any, context: any) => any; @@ -187,37 +189,37 @@ export interface _base { isMatch: (object: object, source: object, customizer?: Function, thisArg?: any) => boolean; - isEmpty: (value: any[]|object|string) => boolean; + isEmpty: (value: any[]|object|string|null|undefined) => boolean; isElement: (value: any) => boolean; - isNumber: (value: any) => boolean; + isNumber: (value: any) => value is number; - isString: (value: any) => boolean; + isString: (value: any) => value is string; - isArray: (value: any) => boolean; + isArray: (value: T[] | any) => value is T[]; - isObject: (value: any) => boolean; + isObject: (value: any) => value is object; - isPlainObject: (value: any) => boolean; + isPlainObject: (value: any) => value is object; isArguments: (value: any) => boolean; - isFunction: (value: any) => boolean; + isFunction: (value: any) => value is Function; - isFinite: (value: any) => boolean; + isFinite: (value: any) => value is number; - isBoolean: (value: any) => boolean; + isBoolean: (value: any) => value is boolean; - isDate: (value: any) => boolean; + isDate: (value: any) => value is Date; - isRegExp: (value: any) => boolean; + isRegExp: (value: any) => value is RegExp; - isError: (value: any) => boolean; + isError: (value: any) => value is Error; - isNaN: (value: any) => boolean; + isNaN: (value: any) => value is number; - isUndefined: (value: any) => boolean; + isUndefined: (value: any) => value is undefined; zipObject: (props: any[], values?: any[]) => object; @@ -239,25 +241,25 @@ export interface _base { freeze: (value: T) => T; - isKey: (key: any) => boolean; + isKey: (key: any) => key is (number | string); isCapitalEqual: (a: string|null|undefined, b: string|null|undefined) => boolean; isWidthOrHeight: (w: number|string) => boolean; - isNotNull: (obj: any) => boolean; + isNotNull: (obj: T) => obj is NonNullable; - isNull: (obj: any) => boolean; + isNull: (obj: any) => obj is (undefined | null); isEmptyArray: (arr: any[]) => boolean; isNotEmptyArray: (arr: any[]) => boolean; - isEmptyObject: (obj: any) => boolean; + isEmptyObject: (obj: any) => obj is object; - isNotEmptyObject: (obj: any) => boolean; + isNotEmptyObject: (obj: any) => obj is object; - isWindow: (obj: any) => boolean; + isWindow: (obj: any) => obj is Window; isDeepMatch: (object: any, attrs: any) => boolean; @@ -349,7 +351,7 @@ export interface _base { isNotEmptyString: (string: string) => boolean; - isEmptyString: (string: any) => boolean; + isEmptyString: (str: any) => str is ""; encrypt: (type: string, text: string, key: string) => string;