From: Evan You Date: Sun, 16 Feb 2020 02:04:29 +0000 (-0500) Subject: fix(types): app.component should accept defineComponent return type X-Git-Tag: v3.0.0-alpha.5~18 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=57ee5df364f03816e548f4f3bf05edc7a089c362;p=thirdparty%2Fvuejs%2Fcore.git fix(types): app.component should accept defineComponent return type fix #730 --- diff --git a/packages/runtime-core/src/apiCreateApp.ts b/packages/runtime-core/src/apiCreateApp.ts index e022d92f95..f8189f25e9 100644 --- a/packages/runtime-core/src/apiCreateApp.ts +++ b/packages/runtime-core/src/apiCreateApp.ts @@ -1,4 +1,9 @@ -import { Component, Data, validateComponentName } from './component' +import { + Component, + Data, + validateComponentName, + PublicAPIComponent +} from './component' import { ComponentOptions } from './apiOptions' import { ComponentPublicInstance } from './componentProxy' import { Directive, validateDirectiveName } from './directives' @@ -82,10 +87,7 @@ export function createAppContext(): AppContext { } export type CreateAppFunction = ( - rootComponent: - | Component - // for compatibility with defineComponent() return types - | { new (): ComponentPublicInstance }, + rootComponent: PublicAPIComponent, rootProps?: Data | null ) => App @@ -156,7 +158,7 @@ export function createAppAPI( return app }, - component(name: string, component?: Component): any { + component(name: string, component?: PublicAPIComponent): any { if (__DEV__) { validateComponentName(name, context.config) } @@ -166,7 +168,7 @@ export function createAppAPI( if (__DEV__ && context.components[name]) { warn(`Component "${name}" has already been registered in target app.`) } - context.components[name] = component + context.components[name] = component as Component return app }, diff --git a/packages/runtime-core/src/apiOptions.ts b/packages/runtime-core/src/apiOptions.ts index 767f5950c1..3ba88f7eda 100644 --- a/packages/runtime-core/src/apiOptions.ts +++ b/packages/runtime-core/src/apiOptions.ts @@ -1,10 +1,10 @@ import { ComponentInternalInstance, Data, - Component, SetupContext, RenderFunction, - SFCInternalOptions + SFCInternalOptions, + PublicAPIComponent } from './component' import { isFunction, @@ -70,10 +70,7 @@ export interface ComponentOptionsBase< push: (item: any) => void, parentInstance: ComponentInternalInstance ) => void - components?: Record< - string, - Component | { new (): ComponentPublicInstance } - > + components?: Record directives?: Record inheritAttrs?: boolean diff --git a/packages/runtime-core/src/component.ts b/packages/runtime-core/src/component.ts index 538ca470ce..d90249a982 100644 --- a/packages/runtime-core/src/component.ts +++ b/packages/runtime-core/src/component.ts @@ -53,6 +53,13 @@ export interface FunctionalComponent

extends SFCInternalOptions { } export type Component = ComponentOptions | FunctionalComponent + +// A type used in public APIs where a component type is expected. +// The constructor type is an artificial type returned by defineComponent(). +export type PublicAPIComponent = + | Component + | { new (): ComponentPublicInstance } + export { ComponentOptions } type LifecycleHook = Function[] | null