Przeglądaj źródła

typings: type-checking WatchHandler

Makes watch handler argument to be typechecked when the expression is set
as a function.
Alexander Sheboltaev 9 lat temu
rodzic
commit
c662fe09c7
3 zmienionych plików z 10 dodań i 5 usunięć
  1. 1 1
      types/index.d.ts
  2. 2 2
      types/options.d.ts
  3. 7 2
      types/vue.d.ts

+ 1 - 1
types/index.d.ts

@@ -15,7 +15,7 @@ declare namespace Vue {
   export type RenderContext = Options.RenderContext;
   export type PropOptions = Options.PropOptions;
   export type ComputedOptions<V extends Vue> = Options.ComputedOptions<V>;
-  export type WatchHandler<V extends Vue> = Options.WatchHandler<V>;
+  export type WatchHandler<V extends Vue> = Options.WatchHandler<V, any>;
   export type WatchOptions = Options.WatchOptions;
   export type DirectiveFunction = Options.DirectiveFunction;
   export type DirectiveOptions = Options.DirectiveOptions;

+ 2 - 2
types/options.d.ts

@@ -17,7 +17,7 @@ export interface ComponentOptions<V extends Vue> {
   propsData?: Object;
   computed?: { [key: string]: ((this: V) => any) | ComputedOptions<V> };
   methods?: { [key: string]: (this: V, ...args: any[]) => any };
-  watch?: { [key: string]: ({ handler: WatchHandler<V> } & WatchOptions) | WatchHandler<V> | string };
+  watch?: { [key: string]: ({ handler: WatchHandler<V, any> } & WatchOptions) | WatchHandler<V, any> | string };
 
   el?: Element | String;
   template?: string;
@@ -75,7 +75,7 @@ export interface ComputedOptions<V> {
   cache?: boolean;
 }
 
-export type WatchHandler<V> = (this: V, val: any, oldVal: any) => void;
+export type WatchHandler<V, T> = (this: V, val: T, oldVal: T) => void;
 
 export interface WatchOptions {
   deep?: boolean;

+ 7 - 2
types/vue.d.ts

@@ -50,8 +50,13 @@ export declare class Vue {
   $set: typeof Vue.set;
   $delete: typeof Vue.delete;
   $watch(
-    expOrFn: string | Function,
-    callback: WatchHandler<this>,
+    expOrFn: string,
+    callback: WatchHandler<this, any>,
+    options?: WatchOptions
+  ): (() => void);
+  $watch<T>(
+    expOrFn: () => T,
+    callback: WatchHandler<this, T>,
     options?: WatchOptions
   ): (() => void);
   $on(event: string, callback: Function): this;