]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
types: improve h inference + infer required props without need for as const
authorEvan You <yyx990803@gmail.com>
Fri, 1 Nov 2019 21:06:19 +0000 (17:06 -0400)
committerEvan You <yyx990803@gmail.com>
Fri, 1 Nov 2019 21:06:19 +0000 (17:06 -0400)
packages/runtime-core/__tests__/apiCreateComponent.spec.tsx
packages/runtime-core/src/apiCreateComponent.ts
packages/runtime-core/src/apiOptions.ts
packages/runtime-core/src/h.ts

index 3fd21122ca666bffdbc530d1b31b2484d0b4d3a4..3918d742cb74817c8604c7a8e4c8aa85ebcac2d8 100644 (file)
@@ -35,7 +35,7 @@ test('createComponent type inference', () => {
         type: Array as () => string[],
         required: true
       }
-    } as const, // required to narrow for conditional check
+    },
     setup(props) {
       props.a && props.a * 2
       props.b.slice()
index 6618837b4df70ae52c890afe67b03d175bc3c1f6..c1503278ee0a4a7b02a264588e0fac4c4f45584a 100644 (file)
@@ -7,18 +7,15 @@ import {
 } from './apiOptions'
 import { SetupContext, RenderFunction } from './component'
 import { ComponentPublicInstance } from './componentProxy'
-import { ExtractPropTypes } from './componentProps'
+import { ExtractPropTypes, ComponentPropsOptions } from './componentProps'
 import { isFunction } from '@vue/shared'
 import { VNodeProps } from './vnode'
 
 // overload 1: direct setup function
 // (uses user defined props interface)
-// __isConstructor: true is a type-only differentiator to avoid returned
-// constructor type from being matched as an options object in h()
 export function createComponent<Props, RawBindings = object>(
   setup: (props: Props, ctx: SetupContext) => RawBindings | RenderFunction
 ): {
-  __isConstructor: true
   new (): ComponentPublicInstance<
     Props,
     RawBindings,
@@ -42,7 +39,6 @@ export function createComponent<
 >(
   options: ComponentOptionsWithoutProps<Props, RawBindings, D, C, M>
 ): {
-  __isConstructor: true
   new (): ComponentPublicInstance<
     Props,
     RawBindings,
@@ -65,7 +61,6 @@ export function createComponent<
 >(
   options: ComponentOptionsWithArrayProps<PropNames, RawBindings, D, C, M>
 ): {
-  __isConstructor: true
   // array props technically doesn't place any contraints on props in TSX
   new (): ComponentPublicInstance<VNodeProps, RawBindings, D, C, M>
 }
@@ -73,7 +68,7 @@ export function createComponent<
 // overload 4: object format with object props declaration
 // see `ExtractPropTypes` in ./componentProps.ts
 export function createComponent<
-  PropsOptions,
+  PropsOptions extends Readonly<ComponentPropsOptions>,
   RawBindings,
   D,
   C extends ComputedOptions = {},
@@ -81,10 +76,9 @@ export function createComponent<
 >(
   options: ComponentOptionsWithObjectProps<PropsOptions, RawBindings, D, C, M>
 ): {
-  __isConstructor: true
   // for Vetur and TSX support
   new (): ComponentPublicInstance<
-    ExtractPropTypes<PropsOptions, false>,
+    ExtractPropTypes<PropsOptions>,
     RawBindings,
     D,
     C,
index 7f764ddc0c04f60698d996f5072e9c435dda1849..d094f50f745850da5719a8a2f957925219336cfb 100644 (file)
@@ -67,8 +67,8 @@ export interface ComponentOptionsBase<
   inheritAttrs?: boolean
 
   // type-only differentiator to separate OptionWihtoutProps from a constructor
-  // type returned by createComponent()
-  __isConstructor?: never
+  // type returned by createComponent() or FunctionalComponent
+  call?: never
 }
 
 export type ComponentOptionsWithoutProps<
index 8b795395233da085fe4d7e9400e1255e5ffe80d0..27045476c353a2bb8d737865f9e21fff68dd2f76 100644 (file)
@@ -1,5 +1,4 @@
 import {
-  VNodeTypes,
   VNode,
   VNodeProps,
   createVNode,
@@ -146,11 +145,7 @@ export function h<P>(
 ): VNode
 
 // Actual implementation
-export function h(
-  type: VNodeTypes,
-  propsOrChildren?: any,
-  children?: any
-): VNode {
+export function h(type: any, propsOrChildren?: any, children?: any): VNode {
   if (arguments.length === 2) {
     if (isObject(propsOrChildren) && !isArray(propsOrChildren)) {
       // single vnode without props