Skip to content

Commit f1c1db9

Browse files
HerringtonDarkholmeaJean
authored andcommitted
fix(types): prefer normal component over functional one (vuejs#7687)
1 parent dc370cf commit f1c1db9

File tree

2 files changed

+11
-8
lines changed

2 files changed

+11
-8
lines changed

types/options.d.ts

+7-4
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,21 @@ export type Accessors<T> = {
3131
[K in keyof T]: (() => T[K]) | ComputedOptions<T[K]>
3232
}
3333

34+
type DataDef<Data, Props, V> = Data | ((this: Readonly<Props> & V) => Data)
3435
/**
3536
* This type should be used when an array of strings is used for a component's `props` value.
3637
*/
3738
export type ThisTypedComponentOptionsWithArrayProps<V extends Vue, Data, Methods, Computed, PropNames extends string> =
3839
object &
39-
ComponentOptions<V, Data | ((this: Readonly<Record<PropNames, any>> & V) => Data), Methods, Computed, PropNames[]> &
40+
ComponentOptions<V, DataDef<Data, Record<PropNames, any>, V>, Methods, Computed, PropNames[], Record<PropNames, any>> &
4041
ThisType<CombinedVueInstance<V, Data, Methods, Computed, Readonly<Record<PropNames, any>>>>;
4142

4243
/**
4344
* This type should be used when an object mapped to `PropOptions` is used for a component's `props` value.
4445
*/
4546
export type ThisTypedComponentOptionsWithRecordProps<V extends Vue, Data, Methods, Computed, Props> =
4647
object &
47-
ComponentOptions<V, Data | ((this: Readonly<Props> & V) => Data), Methods, Computed, RecordPropsDefinition<Props>> &
48+
ComponentOptions<V, DataDef<Data, Props, V>, Methods, Computed, RecordPropsDefinition<Props>, Props> &
4849
ThisType<CombinedVueInstance<V, Data, Methods, Computed, Readonly<Props>>>;
4950

5051
type DefaultData<V> = object | ((this: V) => object);
@@ -56,7 +57,8 @@ export interface ComponentOptions<
5657
Data=DefaultData<V>,
5758
Methods=DefaultMethods<V>,
5859
Computed=DefaultComputed,
59-
PropsDef=PropsDefinition<DefaultProps>> {
60+
PropsDef=PropsDefinition<DefaultProps>,
61+
Props=DefaultProps> {
6062
data?: Data;
6163
props?: PropsDef;
6264
propsData?: object;
@@ -66,7 +68,8 @@ export interface ComponentOptions<
6668

6769
el?: Element | string;
6870
template?: string;
69-
render?(createElement: CreateElement): VNode;
71+
// hack is for funcitonal component type inference, should not used in user code
72+
render?(createElement: CreateElement, hack: RenderContext<Props>): VNode;
7073
renderError?: (h: () => VNode, err: Error) => VNode;
7174
staticRenderFns?: ((createElement: CreateElement) => VNode)[];
7275

types/vue.d.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,10 @@ export interface VueConstructor<V extends Vue = Vue> {
8282
new <Data = object, Methods = object, Computed = object, Props = object>(options?: ThisTypedComponentOptionsWithRecordProps<V, Data, Methods, Computed, Props>): CombinedVueInstance<V, Data, Methods, Computed, Record<keyof Props, any>>;
8383
new (options?: ComponentOptions<V>): CombinedVueInstance<V, object, object, object, Record<keyof object, any>>;
8484

85-
extend<PropNames extends string = never>(definition: FunctionalComponentOptions<Record<PropNames, any>, PropNames[]>): ExtendedVue<V, {}, {}, {}, Record<PropNames, any>>;
86-
extend<Props>(definition: FunctionalComponentOptions<Props, RecordPropsDefinition<Props>>): ExtendedVue<V, {}, {}, {}, Props>;
8785
extend<Data, Methods, Computed, PropNames extends string = never>(options?: ThisTypedComponentOptionsWithArrayProps<V, Data, Methods, Computed, PropNames>): ExtendedVue<V, Data, Methods, Computed, Record<PropNames, any>>;
8886
extend<Data, Methods, Computed, Props>(options?: ThisTypedComponentOptionsWithRecordProps<V, Data, Methods, Computed, Props>): ExtendedVue<V, Data, Methods, Computed, Props>;
87+
extend<PropNames extends string = never>(definition: FunctionalComponentOptions<Record<PropNames, any>, PropNames[]>): ExtendedVue<V, {}, {}, {}, Record<PropNames, any>>;
88+
extend<Props>(definition: FunctionalComponentOptions<Props, RecordPropsDefinition<Props>>): ExtendedVue<V, {}, {}, {}, Props>;
8989
extend(options?: ComponentOptions<V>): ExtendedVue<V, {}, {}, {}, {}>;
9090

9191
nextTick(callback: () => void, context?: any[]): void;
@@ -104,10 +104,10 @@ export interface VueConstructor<V extends Vue = Vue> {
104104
component(id: string): VueConstructor;
105105
component<VC extends VueConstructor>(id: string, constructor: VC): VC;
106106
component<Data, Methods, Computed, Props>(id: string, definition: AsyncComponent<Data, Methods, Computed, Props>): ExtendedVue<V, Data, Methods, Computed, Props>;
107-
component<PropNames extends string>(id: string, definition: FunctionalComponentOptions<Record<PropNames, any>, PropNames[]>): ExtendedVue<V, {}, {}, {}, Record<PropNames, any>>;
108-
component<Props>(id: string, definition: FunctionalComponentOptions<Props, RecordPropsDefinition<Props>>): ExtendedVue<V, {}, {}, {}, Props>;
109107
component<Data, Methods, Computed, PropNames extends string = never>(id: string, definition?: ThisTypedComponentOptionsWithArrayProps<V, Data, Methods, Computed, PropNames>): ExtendedVue<V, Data, Methods, Computed, Record<PropNames, any>>;
110108
component<Data, Methods, Computed, Props>(id: string, definition?: ThisTypedComponentOptionsWithRecordProps<V, Data, Methods, Computed, Props>): ExtendedVue<V, Data, Methods, Computed, Props>;
109+
component<PropNames extends string>(id: string, definition: FunctionalComponentOptions<Record<PropNames, any>, PropNames[]>): ExtendedVue<V, {}, {}, {}, Record<PropNames, any>>;
110+
component<Props>(id: string, definition: FunctionalComponentOptions<Props, RecordPropsDefinition<Props>>): ExtendedVue<V, {}, {}, {}, Props>;
111111
component(id: string, definition?: ComponentOptions<V>): ExtendedVue<V, {}, {}, {}, {}>;
112112

113113
use<T>(plugin: PluginObject<T> | PluginFunction<T>, options?: T): void;

0 commit comments

Comments
 (0)