From 2720836f42d4364f277ee76138264f12c1a66daf Mon Sep 17 00:00:00 2001 From: sh7dm Date: Tue, 29 Oct 2019 22:14:59 +0300 Subject: [PATCH 1/2] refactor(componentProps): clean up code and improve types --- packages/runtime-core/src/componentProps.ts | 26 +++++++++++++-------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/packages/runtime-core/src/componentProps.ts b/packages/runtime-core/src/componentProps.ts index 4490d20196d..9b3aadc78be 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 (): 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) From c766ec9a6c56999b3f6fc3ab5b83f2a2c8162baa Mon Sep 17 00:00:00 2001 From: Dmitry Sharshakov Date: Wed, 30 Oct 2019 08:09:48 +0300 Subject: [PATCH 2/2] Update packages/runtime-core/src/componentProps.ts Co-Authored-By: katashin --- packages/runtime-core/src/componentProps.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/runtime-core/src/componentProps.ts b/packages/runtime-core/src/componentProps.ts index 9b3aadc78be..c1c36e504fe 100644 --- a/packages/runtime-core/src/componentProps.ts +++ b/packages/runtime-core/src/componentProps.ts @@ -42,7 +42,7 @@ interface PropOptions { export type PropType = PropConstructor | PropConstructor[] -type PropConstructor = { new (): T & object } | { (): T } +type PropConstructor = { new (...args: never[]): T & object } | { (): T } type RequiredKeys = { [K in keyof T]: T[K] extends