From ba62deb5d938c43c41ec5e7686dc006a91d809b8 Mon Sep 17 00:00:00 2001 From: Evan You Date: Tue, 9 Oct 2018 13:59:30 -0400 Subject: [PATCH] types: simplify options types --- packages/core/src/component.ts | 4 +--- packages/core/src/componentComputed.ts | 6 ++++-- packages/core/src/componentOptions.ts | 24 ++++++++++++------------ packages/vue/__tests__/compat.spec.ts | 2 +- packages/vue/src/index.ts | 16 +++++----------- 5 files changed, 23 insertions(+), 29 deletions(-) diff --git a/packages/core/src/component.ts b/packages/core/src/component.ts index c221a1dc..5099b396 100644 --- a/packages/core/src/component.ts +++ b/packages/core/src/component.ts @@ -29,8 +29,6 @@ export interface FunctionalComponent

{ export type ComponentType = ComponentClass | FunctionalComponent -// this interface is merged with the class type -// to represent a mounted component export interface ComponentInstance

extends InternalComponent { $vnode: MountedVNode $data: D @@ -40,7 +38,7 @@ export interface ComponentInstance

extends InternalComponent { $slots: Slots $root: ComponentInstance $children: ComponentInstance[] - $options: ComponentOptions + $options: ComponentOptions data?(): Partial render(props: Readonly

, slots: Slots, attrs: Data): any diff --git a/packages/core/src/componentComputed.ts b/packages/core/src/componentComputed.ts index 074adcc9..19fd0ad0 100644 --- a/packages/core/src/componentComputed.ts +++ b/packages/core/src/componentComputed.ts @@ -1,4 +1,4 @@ -import { EMPTY_OBJ } from './utils' +import { EMPTY_OBJ, NOOP } from './utils' import { computed, stop, ComputedGetter } from '@vue/observer' import { ComponentClass, ComponentInstance } from './component' import { ComponentComputedOptions } from './componentOptions' @@ -43,7 +43,9 @@ export function initializeComputed( > = (instance._computedGetters = {}) const proxy = instance.$proxy for (const key in computedOptions) { - handles[key] = computed(computedOptions[key], proxy) + const option = computedOptions[key] + const getter = typeof option === 'function' ? option : option.get || NOOP + handles[key] = computed(getter, proxy) } instance.$computed = new Proxy( {}, diff --git a/packages/core/src/componentOptions.ts b/packages/core/src/componentOptions.ts index eca1c8f9..60bd1cb3 100644 --- a/packages/core/src/componentOptions.ts +++ b/packages/core/src/componentOptions.ts @@ -1,20 +1,14 @@ -import { MergedComponent, ComponentInstance } from './component' +import { ComponentInstance } from './component' import { Slots } from './vdom' export type Data = Record -export interface ComponentOptions< - P = {}, - D = {}, - M = {}, - C = {}, - This = MergedComponent & M & C -> { - data?: (this: This) => Partial - props?: ComponentPropsOptions

+export interface ComponentOptions { + data?(): object + props?: ComponentPropsOptions computed?: ComponentComputedOptions watch?: ComponentWatchOptions - render?: (this: This, props: Readonly

, slots: Slots, attrs: Data) => any + render?: (this: This, props: Readonly, slots: Slots, attrs: Data) => any inheritAttrs?: boolean displayName?: string // TODO other options @@ -39,7 +33,13 @@ export interface PropOptions { } export interface ComponentComputedOptions { - [key: string]: (this: This, c: any) => any + [key: string]: ((this: This, c: This) => any) | SingleComputedOptions +} + +type SingleComputedOptions = { + get: (this: This, c: This) => any + set?: (value: any) => void + cache?: boolean } export interface ComponentWatchOptions { diff --git a/packages/vue/__tests__/compat.spec.ts b/packages/vue/__tests__/compat.spec.ts index 580e202d..b1aa63c2 100644 --- a/packages/vue/__tests__/compat.spec.ts +++ b/packages/vue/__tests__/compat.spec.ts @@ -7,7 +7,7 @@ describe('2.x compat build', async () => { const root = document.createElement('div') document.body.appendChild(root) - const instance = new Vue({ + const instance = new Vue({ data() { return { count: 0 } }, diff --git a/packages/vue/src/index.ts b/packages/vue/src/index.ts index 53b9e2da..72675acf 100644 --- a/packages/vue/src/index.ts +++ b/packages/vue/src/index.ts @@ -3,22 +3,16 @@ import { render, nextTick, Component, - ComponentOptions, createComponentInstance } from '@vue/renderer-dom' -import { MergedComponent } from '../../core/src/component' -class Vue< - P extends object = {}, - D extends object = {}, - M extends object = {}, - C extends object = {} -> extends Component { +// Note: typing for this is intentionally loose, as it will be using 2.x types. +class Vue extends Component { static h = h static render = render static nextTick = nextTick - constructor(options: ComponentOptions & { el?: any }) { + constructor(options: any) { super() if (!options) { return @@ -49,8 +43,8 @@ class Vue< } } -interface Vue { - $mount(el: any): MergedComponent & M & C +interface Vue { + $mount(el: any): any } export default Vue