Explorar el Código

types: improve h inference + infer required props without need for as const

Evan You hace 6 años
padre
commit
7b7b8ef221

+ 1 - 1
packages/runtime-core/__tests__/apiCreateComponent.spec.tsx

@@ -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()

+ 3 - 9
packages/runtime-core/src/apiCreateComponent.ts

@@ -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,

+ 2 - 2
packages/runtime-core/src/apiOptions.ts

@@ -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<

+ 1 - 6
packages/runtime-core/src/h.ts

@@ -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