diff --git a/packages/runtime-core/src/componentProps.ts b/packages/runtime-core/src/componentProps.ts index 4490d20196d..c1c36e504fe 100644 --- a/packages/runtime-core/src/componentProps.ts +++ b/packages/runtime-core/src/componentProps.ts @@ -19,7 +19,7 @@ import { Data, ComponentInternalInstance } from './component' export type ComponentPropsOptions

= | ComponentObjectPropsOptions

- | string[] + | Extract[] export type ComponentObjectPropsOptions

= { [K in keyof P]: Prop | null @@ -27,16 +27,22 @@ export type ComponentObjectPropsOptions

= { export type Prop = PropOptions | PropType +type DefaultFactory = () => T | null | undefined + interface PropOptions { type?: PropType | true | null required?: boolean - default?: T | null | undefined | (() => T | null | undefined) - validator?(value: unknown): boolean + default?: + | DefaultFactory + | null + | undefined + | (T extends Function ? never : T) // If type of prop is function, only allow factory function + validator?(value: T): boolean } export type PropType = PropConstructor | PropConstructor[] -type PropConstructor = { new (...args: any[]): T & object } | { (): T } +type PropConstructor = { new (...args: never[]): T & object } | { (): T } type RequiredKeys = { [K in keyof T]: T[K] extends @@ -282,11 +288,6 @@ function getTypeIndex( return -1 } -type AssertionResult = { - valid: boolean - expectedType: string -} - function validateProp( name: string, value: unknown, @@ -300,7 +301,7 @@ function validateProp( return } // missing but optional - if (value == null && !prop.required) { + if (value == null && !required) { return } // type check @@ -329,6 +330,11 @@ const isSimpleType = /*#__PURE__*/ makeMap( 'String,Number,Boolean,Function,Symbol' ) +type AssertionResult = { + valid: boolean + expectedType: string +} + function assertType(value: unknown, type: PropConstructor): AssertionResult { let valid const expectedType = getType(type)