From e6bbf71fd556f4a501ba6f8531c2f9ea39ee57c7 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Sat, 11 Mar 2023 18:22:29 +0000 Subject: [PATCH] chore: improve internal type safety (#19599) --- packages/nuxt/src/app/composables/router.ts | 8 ++++++-- packages/nuxt/src/app/nuxt.ts | 4 ++-- packages/nuxt/src/pages/runtime/plugins/router.ts | 5 ++++- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/packages/nuxt/src/app/composables/router.ts b/packages/nuxt/src/app/composables/router.ts index ec5897d795..e42db1a63a 100644 --- a/packages/nuxt/src/app/composables/router.ts +++ b/packages/nuxt/src/app/composables/router.ts @@ -1,13 +1,17 @@ import { getCurrentInstance, inject, onUnmounted } from 'vue' +import type { Ref } from 'vue' import type { Router, RouteLocationNormalizedLoaded, NavigationGuard, RouteLocationNormalized, RouteLocationRaw, NavigationFailure, RouteLocationPathRaw } from 'vue-router' import { sendRedirect } from 'h3' import { hasProtocol, joinURL, parseURL } from 'ufo' + import { useNuxtApp, useRuntimeConfig } from '../nuxt' import type { NuxtError } from './error' import { createError } from './error' import { useState } from './state' import { setResponseStatus } from './ssr' +import type { PageMeta } from '#app' + export const useRouter = () => { return useNuxtApp()?.$router as Router } @@ -148,11 +152,11 @@ export const setPageLayout = (layout: string) => { const inMiddleware = isProcessingMiddleware() if (inMiddleware || process.server || nuxtApp.isHydrating) { const unsubscribe = useRouter().beforeResolve((to) => { - to.meta.layout = layout + to.meta.layout = layout as Exclude unsubscribe() }) } if (!inMiddleware) { - useRoute().meta.layout = layout + useRoute().meta.layout = layout as Exclude } } diff --git a/packages/nuxt/src/app/nuxt.ts b/packages/nuxt/src/app/nuxt.ts index 8b904ab112..b6ca3d8103 100644 --- a/packages/nuxt/src/app/nuxt.ts +++ b/packages/nuxt/src/app/nuxt.ts @@ -303,10 +303,10 @@ export function isNuxtPlugin (plugin: unknown) { export function callWithNuxt any> (nuxt: NuxtApp | _NuxtApp, setup: T, args?: Parameters) { const fn: () => ReturnType = () => args ? setup(...args as Parameters) : setup() if (process.server) { - return nuxtAppCtx.callAsync(nuxt, fn) + return nuxtAppCtx.callAsync(nuxt as NuxtApp, fn) } else { // In client side we could assume nuxt app is singleton - nuxtAppCtx.set(nuxt) + nuxtAppCtx.set(nuxt as NuxtApp) return fn() } } diff --git a/packages/nuxt/src/pages/runtime/plugins/router.ts b/packages/nuxt/src/pages/runtime/plugins/router.ts index 80ed21c936..dc06b0f5bf 100644 --- a/packages/nuxt/src/pages/runtime/plugins/router.ts +++ b/packages/nuxt/src/pages/runtime/plugins/router.ts @@ -1,4 +1,5 @@ import { computed, isReadonly, reactive, shallowRef } from 'vue' +import type { Ref } from 'vue' import type { NavigationGuard, RouteLocation @@ -11,6 +12,8 @@ import { } from 'vue-router' import { createError } from 'h3' import { withoutBase, isEqual } from 'ufo' + +import type { PageMeta } from '#app' import { callWithNuxt, defineNuxtPlugin, useRuntimeConfig } from '#app/nuxt' import { showError, clearError, useError } from '#app/composables/error' import { useRequestEvent } from '#app/composables/ssr' @@ -118,7 +121,7 @@ export default defineNuxtPlugin(async (nuxtApp) => { router.beforeEach(async (to, from) => { to.meta = reactive(to.meta) if (nuxtApp.isHydrating && initialLayout.value && !isReadonly(to.meta.layout)) { - to.meta.layout = initialLayout.value + to.meta.layout = initialLayout.value as Exclude } nuxtApp._processingMiddleware = true