Skip to content

Commit 57ee5df

Browse files
committed
fix(types): app.component should accept defineComponent return type
fix #730
1 parent 9d2ac66 commit 57ee5df

File tree

3 files changed

+19
-13
lines changed

3 files changed

+19
-13
lines changed

packages/runtime-core/src/apiCreateApp.ts

+9-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
import { Component, Data, validateComponentName } from './component'
1+
import {
2+
Component,
3+
Data,
4+
validateComponentName,
5+
PublicAPIComponent
6+
} from './component'
27
import { ComponentOptions } from './apiOptions'
38
import { ComponentPublicInstance } from './componentProxy'
49
import { Directive, validateDirectiveName } from './directives'
@@ -82,10 +87,7 @@ export function createAppContext(): AppContext {
8287
}
8388

8489
export type CreateAppFunction<HostElement> = (
85-
rootComponent:
86-
| Component
87-
// for compatibility with defineComponent() return types
88-
| { new (): ComponentPublicInstance<any, any, any, any, any> },
90+
rootComponent: PublicAPIComponent,
8991
rootProps?: Data | null
9092
) => App<HostElement>
9193

@@ -156,7 +158,7 @@ export function createAppAPI<HostNode, HostElement>(
156158
return app
157159
},
158160

159-
component(name: string, component?: Component): any {
161+
component(name: string, component?: PublicAPIComponent): any {
160162
if (__DEV__) {
161163
validateComponentName(name, context.config)
162164
}
@@ -166,7 +168,7 @@ export function createAppAPI<HostNode, HostElement>(
166168
if (__DEV__ && context.components[name]) {
167169
warn(`Component "${name}" has already been registered in target app.`)
168170
}
169-
context.components[name] = component
171+
context.components[name] = component as Component
170172
return app
171173
},
172174

packages/runtime-core/src/apiOptions.ts

+3-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import {
22
ComponentInternalInstance,
33
Data,
4-
Component,
54
SetupContext,
65
RenderFunction,
7-
SFCInternalOptions
6+
SFCInternalOptions,
7+
PublicAPIComponent
88
} from './component'
99
import {
1010
isFunction,
@@ -70,10 +70,7 @@ export interface ComponentOptionsBase<
7070
push: (item: any) => void,
7171
parentInstance: ComponentInternalInstance
7272
) => void
73-
components?: Record<
74-
string,
75-
Component | { new (): ComponentPublicInstance<any, any, any, any, any> }
76-
>
73+
components?: Record<string, PublicAPIComponent>
7774
directives?: Record<string, Directive>
7875
inheritAttrs?: boolean
7976

packages/runtime-core/src/component.ts

+7
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,13 @@ export interface FunctionalComponent<P = {}> extends SFCInternalOptions {
5353
}
5454

5555
export type Component = ComponentOptions | FunctionalComponent
56+
57+
// A type used in public APIs where a component type is expected.
58+
// The constructor type is an artificial type returned by defineComponent().
59+
export type PublicAPIComponent =
60+
| Component
61+
| { new (): ComponentPublicInstance<any, any, any, any, any> }
62+
5663
export { ComponentOptions }
5764

5865
type LifecycleHook = Function[] | null

0 commit comments

Comments
 (0)