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

This commit is contained in:
Evan You 2019-11-01 17:06:19 -04:00
parent b5c501c0b4
commit 7b7b8ef221
4 changed files with 7 additions and 18 deletions

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

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

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

View 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