]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
types: improve typing
authorEvan You <yyx990803@gmail.com>
Tue, 8 Oct 2019 13:26:09 +0000 (09:26 -0400)
committerEvan You <yyx990803@gmail.com>
Tue, 8 Oct 2019 14:50:10 +0000 (10:50 -0400)
packages/runtime-core/src/apiCreateComponent.ts
packages/runtime-core/src/apiOptions.ts
packages/runtime-core/src/componentProps.ts
packages/runtime-core/src/createRenderer.ts
packages/runtime-core/src/h.ts
packages/runtime-core/src/index.ts
packages/runtime-core/src/vnode.ts
packages/runtime-core/src/warning.ts

index 5752da8f066c383af66d99f7cc76ea63805b272c..4f70224dbbeb81cff487853e19a81e5a6f20f7df 100644 (file)
@@ -3,7 +3,7 @@ import {
   MethodOptions,
   ComponentOptionsWithoutProps,
   ComponentOptionsWithArrayProps,
-  ComponentOptionsWithProps
+  ComponentOptionsWithObjectProps
 } from './apiOptions'
 import { SetupContext } from './component'
 import { VNodeChild } from './vnode'
@@ -62,7 +62,7 @@ export function createComponent<
   C extends ComputedOptions = {},
   M extends MethodOptions = {}
 >(
-  options: ComponentOptionsWithProps<PropsOptions, RawBindings, D, C, M>
+  options: ComponentOptionsWithObjectProps<PropsOptions, RawBindings, D, C, M>
 ): {
   // for Vetur and TSX support
   new (): ComponentPublicInstance<
index 87902f7f81b09505f1e7ec2e32fdd99501ce61d3..30b6521d4e8fdcf0d85daf84121567e94f882436 100644 (file)
@@ -27,7 +27,7 @@ import {
   onRenderTriggered
 } from './apiLifecycle'
 import { DebuggerEvent, reactive } from '@vue/reactivity'
-import { ComponentPropsOptions, ExtractPropTypes } from './componentProps'
+import { ComponentObjectPropsOptions, ExtractPropTypes } from './componentProps'
 import { Directive } from './directives'
 import { VNodeChild } from './vnode'
 import { ComponentPublicInstance } from './componentProxy'
@@ -78,8 +78,8 @@ export type ComponentOptionsWithArrayProps<
   props: PropNames[]
 } & ThisType<ComponentPublicInstance<Props, RawBindings, D, C, M>>
 
-export type ComponentOptionsWithProps<
-  PropsOptions = ComponentPropsOptions,
+export type ComponentOptionsWithObjectProps<
+  PropsOptions = ComponentObjectPropsOptions,
   RawBindings = {},
   D = {},
   C extends ComputedOptions = {},
@@ -91,7 +91,7 @@ export type ComponentOptionsWithProps<
 
 export type ComponentOptions =
   | ComponentOptionsWithoutProps
-  | ComponentOptionsWithProps
+  | ComponentOptionsWithObjectProps
   | ComponentOptionsWithArrayProps
 
 // TODO legacy component definition also supports constructors with .options
index 511e66dd56c33d4751dcf1ddf447d6754e5c405a..25e00ee2fac9348ccbd2e0aaf91a2a73053fee7e 100644 (file)
@@ -16,7 +16,11 @@ import {
 import { warn } from './warning'
 import { Data, ComponentInternalInstance } from './component'
 
-export type ComponentPropsOptions<P = Data> = {
+export type ComponentPropsOptions<P = Data> =
+  | ComponentObjectPropsOptions<P>
+  | string[]
+
+export type ComponentObjectPropsOptions<P = Data> = {
   [K in keyof P]: Prop<P[K]> | null
 }
 
index 00280955cff4516580d2113e6fb742f5143b9497..2d230ec712e9d56c5d478e9fcb9ddc3b2cd59914 100644 (file)
@@ -13,7 +13,8 @@ import {
   ComponentInternalInstance,
   createComponentInstance,
   setupStatefulComponent,
-  handleSetupResult
+  handleSetupResult,
+  Component
 } from './component'
 import {
   renderComponentRoot,
@@ -1006,7 +1007,7 @@ export function createRenderer<
     }
 
     // resolve props and slots for setup context
-    const propsOptions = (initialVNode.type as any).props
+    const propsOptions = (initialVNode.type as Component).props
     resolveProps(instance, initialVNode.props, propsOptions)
     resolveSlots(instance, initialVNode.children)
 
index 6c9c8c9bd8bfac243769ae0bda0ead7b83801a00..3a29607a586fbae36dc541599d9ce97c6dfc58eb 100644 (file)
@@ -13,7 +13,7 @@ import { FunctionalComponent } from './component'
 import {
   ComponentOptionsWithoutProps,
   ComponentOptionsWithArrayProps,
-  ComponentOptionsWithProps,
+  ComponentOptionsWithObjectProps,
   ComponentOptions
 } from './apiOptions'
 import { ExtractPropTypes } from './componentProps'
@@ -121,7 +121,7 @@ export function h<P extends string>(
   children?: RawChildren | RawSlots
 ): VNode
 export function h<P>(
-  type: ComponentOptionsWithProps<P>,
+  type: ComponentOptionsWithObjectProps<P>,
   props?: (RawProps & ExtractPropTypes<P>) | null,
   children?: RawChildren | RawSlots
 ): VNode
index 25204c10e12446feffafe1b19659072e1d1ff223..7ce44b9a7f66cebd54ccc9746a4b61872907f3fa 100644 (file)
@@ -63,14 +63,19 @@ export {
 export {
   ComponentOptions,
   ComponentOptionsWithoutProps,
-  ComponentOptionsWithProps,
+  ComponentOptionsWithObjectProps as ComponentOptionsWithProps,
   ComponentOptionsWithArrayProps
 } from './apiOptions'
 
 export { ComponentPublicInstance } from './componentProxy'
 export { RendererOptions } from './createRenderer'
 export { Slot, Slots } from './componentSlots'
-export { Prop, PropType, ComponentPropsOptions } from './componentProps'
+export {
+  Prop,
+  PropType,
+  ComponentPropsOptions,
+  ComponentObjectPropsOptions
+} from './componentProps'
 export {
   Directive,
   DirectiveBinding,
index 3a74985c715ad7dd1a532f79d2c23a102da1f772..2229fbcdc313a1538c353a775bec51e887149c71 100644 (file)
@@ -7,7 +7,12 @@ import {
   extend,
   PatchFlags
 } from '@vue/shared'
-import { ComponentInternalInstance, Data, SetupProxySymbol } from './component'
+import {
+  ComponentInternalInstance,
+  Data,
+  SetupProxySymbol,
+  Component
+} from './component'
 import { RawSlots } from './componentSlots'
 import { ShapeFlags } from './shapeFlags'
 import { isReactive } from '@vue/reactivity'
@@ -22,8 +27,7 @@ export const Suspense = __DEV__ ? Symbol('Suspense') : Symbol()
 
 export type VNodeTypes =
   | string
-  | Function
-  | Object
+  | Component
   | typeof Fragment
   | typeof Portal
   | typeof Text
index e1d6f9e9a9539341b1d5fbf3431b9cf75a0f8426..09cf34e585aeef46c38bf76c94a4fafb6ccbe8ad 100644 (file)
@@ -1,18 +1,22 @@
 import { VNode } from './vnode'
-import { Data, ComponentInternalInstance } from './component'
-import { isString } from '@vue/shared'
+import { Data, ComponentInternalInstance, Component } from './component'
+import { isString, isFunction } from '@vue/shared'
 import { toRaw } from '@vue/reactivity'
 
+type ComponentVNode = VNode & {
+  type: Component
+}
+
 let stack: VNode[] = []
 
 type TraceEntry = {
-  vnode: VNode
+  vnode: ComponentVNode
   recurseCount: number
 }
 
 type ComponentTraceStack = TraceEntry[]
 
-export function pushWarningContext(vnode: VNode) {
+export function pushWarningContext(vnode: ComponentVNode) {
   stack.push(vnode)
 }
 
@@ -117,9 +121,9 @@ const classifyRE = /(?:^|[-_])(\w)/g
 const classify = (str: string): string =>
   str.replace(classifyRE, c => c.toUpperCase()).replace(/[-_]/g, '')
 
-function formatComponentName(vnode: VNode, file?: string): string {
-  const Component = vnode.type as any
-  let name = Component.displayName || Component.name
+function formatComponentName(vnode: ComponentVNode, file?: string): string {
+  const Component = vnode.type
+  let name = isFunction(Component) ? Component.displayName : Component.name
   if (!name && file) {
     const match = file.match(/([^/\\]+)\.vue$/)
     if (match) {
@@ -136,7 +140,7 @@ function formatProps(props: Data): string[] {
     if (isString(value)) {
       res.push(`${key}=${JSON.stringify(value)}`)
     } else {
-      res.push(`${key}=`, toRaw(value) as any)
+      res.push(`${key}=`, String(toRaw(value)))
     }
   }
   return res