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 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 { export interface ComponentInstance<P = {}, D = {}> extends InternalComponent {
$vnode: MountedVNode $vnode: MountedVNode
$data: D $data: D
@ -40,7 +38,7 @@ export interface ComponentInstance<P = {}, D = {}> extends InternalComponent {
$slots: Slots $slots: Slots
$root: ComponentInstance $root: ComponentInstance
$children: ComponentInstance[] $children: ComponentInstance[]
$options: ComponentOptions<P, D> $options: ComponentOptions<this>
data?(): Partial<D> data?(): Partial<D>
render(props: Readonly<P>, slots: Slots, attrs: Data): any 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 { computed, stop, ComputedGetter } from '@vue/observer'
import { ComponentClass, ComponentInstance } from './component' import { ComponentClass, ComponentInstance } from './component'
import { ComponentComputedOptions } from './componentOptions' import { ComponentComputedOptions } from './componentOptions'
@ -43,7 +43,9 @@ export function initializeComputed(
> = (instance._computedGetters = {}) > = (instance._computedGetters = {})
const proxy = instance.$proxy const proxy = instance.$proxy
for (const key in computedOptions) { 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( instance.$computed = new Proxy(
{}, {},

View File

@ -1,20 +1,14 @@
import { MergedComponent, ComponentInstance } from './component' import { ComponentInstance } from './component'
import { Slots } from './vdom' import { Slots } from './vdom'
export type Data = Record<string, any> export type Data = Record<string, any>
export interface ComponentOptions< export interface ComponentOptions<This = ComponentInstance> {
P = {}, data?(): object
D = {}, props?: ComponentPropsOptions
M = {},
C = {},
This = MergedComponent<P, D> & M & C
> {
data?: (this: This) => Partial<D>
props?: ComponentPropsOptions<P>
computed?: ComponentComputedOptions<This> computed?: ComponentComputedOptions<This>
watch?: ComponentWatchOptions<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 inheritAttrs?: boolean
displayName?: string displayName?: string
// TODO other options // TODO other options
@ -39,7 +33,13 @@ export interface PropOptions<T = any> {
} }
export interface ComponentComputedOptions<This = ComponentInstance> { 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> { export interface ComponentWatchOptions<This = ComponentInstance> {

View File

@ -7,7 +7,7 @@ describe('2.x compat build', async () => {
const root = document.createElement('div') const root = document.createElement('div')
document.body.appendChild(root) document.body.appendChild(root)
const instance = new Vue<any>({ const instance = new Vue({
data() { data() {
return { count: 0 } return { count: 0 }
}, },

View File

@ -3,22 +3,16 @@ import {
render, render,
nextTick, nextTick,
Component, Component,
ComponentOptions,
createComponentInstance createComponentInstance
} from '@vue/renderer-dom' } from '@vue/renderer-dom'
import { MergedComponent } from '../../core/src/component'
class Vue< // Note: typing for this is intentionally loose, as it will be using 2.x types.
P extends object = {}, class Vue extends Component {
D extends object = {},
M extends object = {},
C extends object = {}
> extends Component {
static h = h static h = h
static render = render static render = render
static nextTick = nextTick static nextTick = nextTick
constructor(options: ComponentOptions<P, D, M, C> & { el?: any }) { constructor(options: any) {
super() super()
if (!options) { if (!options) {
return return
@ -49,8 +43,8 @@ class Vue<
} }
} }
interface Vue<P, D, M, C> { interface Vue {
$mount(el: any): MergedComponent<P, D> & M & C $mount(el: any): any
} }
export default Vue export default Vue