types: simplify options types

This commit is contained in:
Evan You
2018-10-09 13:59:30 -04:00
parent e698c8f492
commit ba62deb5d9
5 changed files with 23 additions and 29 deletions

View File

@@ -29,8 +29,6 @@ export interface FunctionalComponent<P = {}> {
export type ComponentType = ComponentClass | FunctionalComponent
// this interface is merged with the class type
// to represent a mounted component
export interface ComponentInstance<P = {}, D = {}> extends InternalComponent {
$vnode: MountedVNode
$data: D
@@ -40,7 +38,7 @@ export interface ComponentInstance<P = {}, D = {}> extends InternalComponent {
$slots: Slots
$root: ComponentInstance
$children: ComponentInstance[]
$options: ComponentOptions<P, D>
$options: ComponentOptions<this>
data?(): Partial<D>
render(props: Readonly<P>, slots: Slots, attrs: Data): any

View File

@@ -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(
{},

View File

@@ -1,20 +1,14 @@
import { MergedComponent, ComponentInstance } from './component'
import { ComponentInstance } from './component'
import { Slots } from './vdom'
export type Data = Record<string, any>
export interface ComponentOptions<
P = {},
D = {},
M = {},
C = {},
This = MergedComponent<P, D> & M & C
> {
data?: (this: This) => Partial<D>
props?: ComponentPropsOptions<P>
export interface ComponentOptions<This = ComponentInstance> {
data?(): object
props?: ComponentPropsOptions
computed?: ComponentComputedOptions<This>
watch?: ComponentWatchOptions<This>
render?: (this: This, props: Readonly<P>, slots: Slots, attrs: Data) => any
render?: (this: This, props: Readonly<Data>, slots: Slots, attrs: Data) => any
inheritAttrs?: boolean
displayName?: string
// TODO other options
@@ -39,7 +33,13 @@ export interface PropOptions<T = any> {
}
export interface ComponentComputedOptions<This = ComponentInstance> {
[key: string]: (this: This, c: any) => any
[key: string]: ((this: This, c: This) => any) | SingleComputedOptions<This>
}
type SingleComputedOptions<This> = {
get: (this: This, c: This) => any
set?: (value: any) => void
cache?: boolean
}
export interface ComponentWatchOptions<This = ComponentInstance> {