Browse Source

declare component options shape

Evan You 10 years ago
parent
commit
7b3ba65f4e

+ 1 - 1
flow/component.js

@@ -17,7 +17,7 @@ declare interface Component {
   // public properties
   $el: Element | void;
   $data: Object;
-  $options: Object;
+  $options: ComponentOptions;
   $parent: Component | void;
   $root: Component;
   $children: Array<Component>;

+ 52 - 0
flow/options.js

@@ -1,3 +1,55 @@
 declare type ComponentOptions = {
+  // data
+  data: Object | Function | void,
+  props?: { [key: string]: PropOptions },
+  propsData?: Object,
+  computed?: {
+    [key: string]: Function | {
+      get?: Function,
+      set?: Function,
+      cache?: boolean
+    }
+  },
+  methods?: {
+    [key: string]: Function
+  },
+  watch?: {
+    [key: string]: Function | string
+  },
+  // DOM
+  el?: string | Element,
+  template?: string,
+  render: () => VNode,
+  staticRenderFns?: Array<() => VNode>,
+  // lifecycle
+  init?: Function,
+  created?: Function,
+  beforeMount?: Function,
+  mounted?: Function,
+  beforeUpdate?: Function,
+  updated?: Function,
+  // assets
+  directives?: { [key: string]: Object },
+  components?: { [key: string]: Class<Component> },
+  transitions?: { [key: string]: Object },
+  filters?: { [key: string]: Function },
+  // misc
+  parent?: Component,
+  mixins?: Array<Object>,
+  name?: string,
+  extends?: Class<Component> | Object,
+  delimiters?: [string, string],
 
+  // private
+  _propKeys?: Array<string>,
+  _parentVnode?: VNode,
+  _parentListeners?: ?{ [key: string]: Function | Array<Function> },
+  _renderChildren?: ?VNodeChildren
+}
+
+declare type PropOptions = {
+  type: Function | Array<Function> | null,
+  default: any,
+  required: ?boolean,
+  validator: ?Function
 }

+ 1 - 1
src/core/instance/init.js

@@ -9,7 +9,7 @@ import { mergeOptions } from '../util/index'
 
 let uid = 0
 
-export function init (vm: Component, options?: Object) {
+export function init (vm: Component, options?: ComponentOptions) {
   // a uid
   vm._uid = uid++
   // a flag to avoid this being observed

+ 1 - 1
src/core/instance/lifecycle.js

@@ -89,7 +89,7 @@ export function lifecycleMixin (Vue: Class<Component>) {
     // update props
     if (propsData && vm.$options.props) {
       observerState.shouldConvert = false
-      const propKeys = vm.$options.propKeys
+      const propKeys = vm.$options._propKeys || []
       for (let i = 0; i < propKeys.length; i++) {
         const key = propKeys[i]
         vm[key] = validateProp(vm, key, propsData)

+ 1 - 1
src/core/instance/state.js

@@ -31,7 +31,7 @@ function initProps (vm: Component) {
   const props = vm.$options.props
   const propsData = vm.$options.propsData
   if (props) {
-    const keys = vm.$options.propKeys = Object.keys(props)
+    const keys = vm.$options._propKeys = Object.keys(props)
     const isRoot = !vm.$parent
     // root instance props should be converted
     observerState.shouldConvert = isRoot

+ 1 - 1
src/core/util/props.js

@@ -12,7 +12,7 @@ type PropOptions = {
 }
 
 export function validateProp (vm: Component, key: string, propsData: ?Object): any {
-  if (!propsData) return
+  if (!vm.$options.props || !propsData) return
   const prop = vm.$options.props[key]
   const absent = hasOwn(propsData, key)
   let value = propsData[key]