Skip to content

Commit 1f423d6

Browse files
feat(typing): use keyof inference to simplify definition
1 parent dfaf9e2 commit 1f423d6

File tree

2 files changed

+10
-26
lines changed

2 files changed

+10
-26
lines changed

types/options.d.ts

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,20 +44,10 @@ export type Accessors<T> = {
4444
}
4545

4646
type DataDef<Data, Props, V> = Data | ((this: Readonly<Props> & V) => Data)
47-
/**
48-
* This type should be used when an array of strings is used for a component's `props` value.
49-
*/
50-
export type ThisTypedComponentOptionsWithArrayProps<V extends Vue, Data, Methods, Computed, PropNames extends string> =
51-
object &
52-
ComponentOptions<V, DataDef<Data, Record<PropNames, any>, V>, Methods, Computed, PropNames[], Record<PropNames, any>> &
53-
ThisType<CombinedVueInstance<V, Data, Methods, Computed, Readonly<Record<PropNames, any>>>>;
5447

55-
/**
56-
* This type should be used when an object mapped to `PropOptions` is used for a component's `props` value.
57-
*/
58-
export type ThisTypedComponentOptionsWithRecordProps<V extends Vue, Data, Methods, Computed, Props> =
48+
export type ThisTypedComponentOptions<V extends Vue, Data, Methods, Computed, Props> =
5949
object &
60-
ComponentOptions<V, DataDef<Data, Props, V>, Methods, Computed, RecordPropsDefinition<Props>, Props> &
50+
ComponentOptions<V, DataDef<Data, Props, V>, Methods, Computed, PropsDefinition<Props>, Props> &
6151
ThisType<CombinedVueInstance<V, Data, Methods, Computed, Readonly<Props>>>;
6252

6353
type DefaultData<V> = object | ((this: V) => object);

types/vue.d.ts

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@ import {
77
WatchHandler,
88
DirectiveOptions,
99
DirectiveFunction,
10-
RecordPropsDefinition,
11-
ThisTypedComponentOptionsWithArrayProps,
12-
ThisTypedComponentOptionsWithRecordProps,
10+
PropsDefinition,
11+
ThisTypedComponentOptions,
1312
WatchOptions,
1413
} from "./options";
1514
import { VNode, VNodeData, VNodeChildren, ScopedSlot } from "./vnode";
@@ -78,15 +77,12 @@ export interface VueConfiguration {
7877
}
7978

8079
export interface VueConstructor<V extends Vue = Vue> {
81-
new <Data = object, Methods = object, Computed = object, PropNames extends string = never>(options?: ThisTypedComponentOptionsWithArrayProps<V, Data, Methods, Computed, PropNames>): CombinedVueInstance<V, Data, Methods, Computed, Record<PropNames, any>>;
82-
// ideally, the return type should just contain Props, not Record<keyof Props, any>. But TS requires to have Base constructors with the same return type.
83-
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>>;
80+
// ideally, the return type should just contains Props, not Record<keyof Props, any>. But TS requires Base constructors must all have the same return type.
81+
new <Data = object, Methods = object, Computed = object, Props = object>(options?: ThisTypedComponentOptions<V, Data, Methods, Computed, Props>): CombinedVueInstance<V, Data, Methods, Computed, Record<keyof Props, any>>;
8482
new (options?: ComponentOptions<V>): CombinedVueInstance<V, object, object, object, Record<keyof object, any>>;
8583

86-
extend<Data, Methods, Computed, PropNames extends string = never>(options?: ThisTypedComponentOptionsWithArrayProps<V, Data, Methods, Computed, PropNames>): ExtendedVue<V, Data, Methods, Computed, Record<PropNames, any>>;
87-
extend<Data, Methods, Computed, Props>(options?: ThisTypedComponentOptionsWithRecordProps<V, Data, Methods, Computed, Props>): ExtendedVue<V, Data, Methods, Computed, Props>;
88-
extend<PropNames extends string = never>(definition: FunctionalComponentOptions<Record<PropNames, any>, PropNames[]>): ExtendedVue<V, {}, {}, {}, Record<PropNames, any>>;
89-
extend<Props>(definition: FunctionalComponentOptions<Props, RecordPropsDefinition<Props>>): ExtendedVue<V, {}, {}, {}, Props>;
84+
extend<Data, Methods, Computed, Props>(options?: ThisTypedComponentOptions<V, Data, Methods, Computed, Props>): ExtendedVue<V, Data, Methods, Computed, Props>;
85+
extend<Props>(definition: FunctionalComponentOptions<Props, PropsDefinition<Props>>): ExtendedVue<V, {}, {}, {}, Props>;
9086
extend(options?: ComponentOptions<V>): ExtendedVue<V, {}, {}, {}, {}>;
9187

9288
nextTick(callback: () => void, context?: any[]): void;
@@ -105,10 +101,8 @@ export interface VueConstructor<V extends Vue = Vue> {
105101
component(id: string): VueConstructor;
106102
component<VC extends VueConstructor>(id: string, constructor: VC): VC;
107103
component<Data, Methods, Computed, Props>(id: string, definition: AsyncComponent<Data, Methods, Computed, Props>): ExtendedVue<V, Data, Methods, Computed, Props>;
108-
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>>;
109-
component<Data, Methods, Computed, Props>(id: string, definition?: ThisTypedComponentOptionsWithRecordProps<V, Data, Methods, Computed, Props>): ExtendedVue<V, Data, Methods, Computed, Props>;
110-
component<PropNames extends string>(id: string, definition: FunctionalComponentOptions<Record<PropNames, any>, PropNames[]>): ExtendedVue<V, {}, {}, {}, Record<PropNames, any>>;
111-
component<Props>(id: string, definition: FunctionalComponentOptions<Props, RecordPropsDefinition<Props>>): ExtendedVue<V, {}, {}, {}, Props>;
104+
component<Data, Methods, Computed, Props>(id: string, definition?: ThisTypedComponentOptions<V, Data, Methods, Computed, Props>): ExtendedVue<V, Data, Methods, Computed, Props>;
105+
component<Props>(id: string, definition: FunctionalComponentOptions<Props, PropsDefinition<Props>>): ExtendedVue<V, {}, {}, {}, Props>;
112106
component(id: string, definition?: ComponentOptions<V>): ExtendedVue<V, {}, {}, {}, {}>;
113107

114108
use<T>(plugin: PluginObject<T> | PluginFunction<T>, options?: T): this;

0 commit comments

Comments
 (0)