diff --git a/dist/common/class-names.js b/dist/common/class-names.js index 7a3a99f1..b7823b3e 100644 --- a/dist/common/class-names.js +++ b/dist/common/class-names.js @@ -1,29 +1,28 @@ const hasOwn = {}.hasOwnProperty; - export function classNames() { - const classes = []; - - for (let i = 0; i < arguments.length; i++) { - const arg = arguments[i]; - if (!arg) continue; - - const argType = typeof arg; - - if (argType === 'string' || argType === 'number') { - classes.push(arg); - } else if (Array.isArray(arg) && arg.length) { - const inner = classNames.apply(null, arg); - if (inner) { - classes.push(inner); - } - } else if (argType === 'object') { - for (const key in arg) { - if (hasOwn.call(arg, key) && arg[key]) { - classes.push(key); + const classes = []; + for (let i = 0; i < arguments.length; i++) { + const arg = arguments[i]; + if (!arg) + continue; + const argType = typeof arg; + if (argType === 'string' || argType === 'number') { + classes.push(arg); + } + else if (Array.isArray(arg) && arg.length) { + const inner = classNames.apply(null, arg); + if (inner) { + classes.push(inner); + } + } + else if (argType === 'object') { + for (const key in arg) { + if (hasOwn.call(arg, key) && arg[key]) { + classes.push(key); + } + } } - } } - } - - return classes.join(' '); -}; + return classes.join(' '); +} +; diff --git a/packages/button/index.ts b/packages/button/index.ts index 8f14ac3b..dd64a65d 100644 --- a/packages/button/index.ts +++ b/packages/button/index.ts @@ -21,7 +21,7 @@ VantComponent({ }, computed: { - classes() { + classes(): string { const { type, size, plain, disabled, loading, square, block } = this.data; return this.classNames(`van-button--${type}`, `van-button--${size}`, { 'van-button--block': block, diff --git a/packages/common/class-names.js b/packages/common/class-names.ts similarity index 94% rename from packages/common/class-names.js rename to packages/common/class-names.ts index 7a3a99f1..5c8e65d0 100644 --- a/packages/common/class-names.js +++ b/packages/common/class-names.ts @@ -1,6 +1,6 @@ const hasOwn = {}.hasOwnProperty; -export function classNames() { +export function classNames(): string { const classes = []; for (let i = 0; i < arguments.length; i++) { diff --git a/types/class-names.d.ts b/types/class-names.d.ts new file mode 100644 index 00000000..c3e3d325 --- /dev/null +++ b/types/class-names.d.ts @@ -0,0 +1,16 @@ +type ClassValue = + | string + | number + | ClassDictionary + | ClassArray + | undefined + | null + | boolean; + +interface ClassDictionary { + [id: string]: any; +} + +interface ClassArray extends Array {} + +export type classNames = (...classes: ClassValue[]) => string; diff --git a/types/index.d.ts b/types/index.d.ts index 4ba85ffb..eb6315e5 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -1,5 +1,5 @@ /// -import { Vue } from './vue'; +import { ComponentInstance } from './instance'; type Mixins = any[]; type ExternalClasses = string[]; @@ -14,12 +14,20 @@ type Relations = { }; }; type RecordToAny = { [K in keyof T]: any }; +type Accessors = { + [K in keyof T]: (() => T[K]) +} -export type CombinedComponentInstance = Vue & - Methods & +export type CombinedComponentInstance< + Data, + Props, + Methods, + Computed +> = Methods & LooseObject & - Weapp.Component & { - data: Data & RecordToAny & RecordToAny; + Weapp.Component & + ComponentInstance & { + data: Data & RecordToAny & Computed; }; export type VantComponentOptions = { @@ -27,7 +35,7 @@ export type VantComponentOptions = { props?: Props; field?: boolean; mixins?: Mixins; - computed?: Computed & ThisType; + computed?: Accessors & ThisType; relations?: Relations; classes?: ExternalClasses; methods?: Methods & ThisType; diff --git a/types/instance.ts b/types/instance.ts new file mode 100644 index 00000000..952adf01 --- /dev/null +++ b/types/instance.ts @@ -0,0 +1,6 @@ +import { classNames } from './class-names'; + +export interface ComponentInstance { + $emit(name: string, detail?: any): void; + classNames: classNames; +} diff --git a/types/vue.d.ts b/types/vue.d.ts deleted file mode 100644 index 829fc0eb..00000000 --- a/types/vue.d.ts +++ /dev/null @@ -1,3 +0,0 @@ -export interface Vue { - $emit(name: string, detail?: any): void; -}