]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
types: simplify options types
authorEvan You <yyx990803@gmail.com>
Tue, 9 Oct 2018 17:59:30 +0000 (13:59 -0400)
committerEvan You <yyx990803@gmail.com>
Tue, 9 Oct 2018 17:59:30 +0000 (13:59 -0400)
packages/core/src/component.ts
packages/core/src/componentComputed.ts
packages/core/src/componentOptions.ts
packages/vue/__tests__/compat.spec.ts
packages/vue/src/index.ts

index c221a1dcd0ba5b6b942a58cac3a33da42f403b09..5099b39675287a6884a242ec75dea3b4609b800b 100644 (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
index 074adcc9e8f7b7f69c3864fdfd7428f96f2ea119..19fd0ad0dd4d0a978c15333ff76b521ebf8bd705 100644 (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(
     {},
index eca1c8f9d08c4541132fb472b18a472736254cc7..60bd1cb306c22740990344b093bf727a92ac3ebe 100644 (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> {
index 580e202d7eedc258a05a5069566b36b6d55fc3ac..b1aa63c26ce5fecc46d251d422a38333e1ca7327 100644 (file)
@@ -7,7 +7,7 @@ describe('2.x compat build', async () => {
     const root = document.createElement('div')
     document.body.appendChild(root)
 
-    const instance = new Vue<any>({
+    const instance = new Vue({
       data() {
         return { count: 0 }
       },
index 53b9e2dabad6c59a08db10025db9bc9e6235c79b..72675acf954c8eca5fbe8fab02a2bc67d70291c9 100644 (file)
@@ -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<P, D, M, C> & { el?: any }) {
+  constructor(options: any) {
     super()
     if (!options) {
       return
@@ -49,8 +43,8 @@ class Vue<
   }
 }
 
-interface Vue<P, D, M, C> {
-  $mount(el: any): MergedComponent<P, D> & M & C
+interface Vue {
+  $mount(el: any): any
 }
 
 export default Vue