From da0a3794ef817bd841a6433123450fd6c2b05498 Mon Sep 17 00:00:00 2001 From: Hartmut Date: Fri, 14 Dec 2018 02:31:25 +0000 Subject: [PATCH] fix(vue-app): add type definition for `ComponentOptions.middleware` (#4531) --- packages/vue-app/types/index.d.ts | 2 ++ packages/vue-app/types/vue.d.ts | 4 ++-- test/types/index.ts | 14 ++++++++++++-- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/packages/vue-app/types/index.d.ts b/packages/vue-app/types/index.d.ts index 79890d79b1..183a6cb35d 100644 --- a/packages/vue-app/types/index.d.ts +++ b/packages/vue-app/types/index.d.ts @@ -30,6 +30,8 @@ export interface Context { beforeNuxtRender(fn: (params: { Components: VueRouter['getMatchedComponents'], nuxtState: NuxtState }) => void): void } +export type Middleware = string | ((ctx: Context, cb: Function) => Promise | void) + export interface Transition { name?: string; mode?: string; diff --git a/packages/vue-app/types/vue.d.ts b/packages/vue-app/types/vue.d.ts index 92656cb118..ee24895b10 100644 --- a/packages/vue-app/types/vue.d.ts +++ b/packages/vue-app/types/vue.d.ts @@ -5,7 +5,7 @@ import Vue, { ComponentOptions } from "vue"; import { Route } from "vue-router"; import { MetaInfo } from "vue-meta"; -import { Context, Transition, LoadingObject } from "./index"; +import { Context, Middleware, Transition, LoadingObject } from "./index"; declare module "vue/types/options" { interface ComponentOptions { @@ -14,7 +14,7 @@ declare module "vue/types/options" { head?: MetaInfo | (() => MetaInfo); key?: string | ((to: Route) => string); layout?: string | ((ctx: Context) => string); - middleware?: string | string[]; + middleware?: Middleware | Middleware[]; scrollToTop?: boolean; transition?: string | Transition | ((to: Route, from: Route) => string); validate?(ctx: Context): Promise | boolean; diff --git a/test/types/index.ts b/test/types/index.ts index 219e8febd5..482a984778 100644 --- a/test/types/index.ts +++ b/test/types/index.ts @@ -50,8 +50,18 @@ options.layout = (context) => 'foo' // middleware -options.middleware = 'foo' -options.middleware = ['foo', 'bar'] +const middlewares: types.Middleware[] = [ + 'foo', + (ctx) => {}, + (ctx, cb) => {}, + async (ctx) => {}, + async (ctx, cb) => {} // unlikely +] + +options.middleware = middlewares +options.middleware = middlewares[0] +options.middleware = middlewares[1] +options.middleware = middlewares[2] // scrollToTop