Skip to content

Commit

Permalink
fix(types): app.component should accept defineComponent return type
Browse files Browse the repository at this point in the history
fix #730
  • Loading branch information
yyx990803 committed Feb 16, 2020
1 parent 9d2ac66 commit 57ee5df
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 13 deletions.
16 changes: 9 additions & 7 deletions packages/runtime-core/src/apiCreateApp.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -82,10 +87,7 @@ export function createAppContext(): AppContext {
}

export type CreateAppFunction<HostElement> = (
rootComponent:
| Component
// for compatibility with defineComponent() return types
| { new (): ComponentPublicInstance<any, any, any, any, any> },
rootComponent: PublicAPIComponent,
rootProps?: Data | null
) => App<HostElement>

Expand Down Expand Up @@ -156,7 +158,7 @@ export function createAppAPI<HostNode, HostElement>(
return app
},

component(name: string, component?: Component): any {
component(name: string, component?: PublicAPIComponent): any {
if (__DEV__) {
validateComponentName(name, context.config)
}
Expand All @@ -166,7 +168,7 @@ export function createAppAPI<HostNode, HostElement>(
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
},

Expand Down
9 changes: 3 additions & 6 deletions packages/runtime-core/src/apiOptions.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import {
ComponentInternalInstance,
Data,
Component,
SetupContext,
RenderFunction,
SFCInternalOptions
SFCInternalOptions,
PublicAPIComponent
} from './component'
import {
isFunction,
Expand Down Expand Up @@ -70,10 +70,7 @@ export interface ComponentOptionsBase<
push: (item: any) => void,
parentInstance: ComponentInternalInstance
) => void
components?: Record<
string,
Component | { new (): ComponentPublicInstance<any, any, any, any, any> }
>
components?: Record<string, PublicAPIComponent>
directives?: Record<string, Directive>
inheritAttrs?: boolean

Expand Down
7 changes: 7 additions & 0 deletions packages/runtime-core/src/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ export interface FunctionalComponent<P = {}> 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<any, any, any, any, any> }

export { ComponentOptions }

type LifecycleHook = Function[] | null
Expand Down

0 comments on commit 57ee5df

Please sign in to comment.