diff --git a/packages/nuxt/src/core/nuxt.ts b/packages/nuxt/src/core/nuxt.ts index e319d53d7c..b64525af10 100644 --- a/packages/nuxt/src/core/nuxt.ts +++ b/packages/nuxt/src/core/nuxt.ts @@ -125,6 +125,7 @@ async function initNuxt (nuxt: Nuxt) { // Add nuxt types nuxt.hook('prepare:types', (opts) => { opts.references.push({ types: 'nuxt' }) + opts.references.push({ path: resolve(nuxt.options.buildDir, 'types/app-defaults.d.ts') }) opts.references.push({ path: resolve(nuxt.options.buildDir, 'types/plugins.d.ts') }) // Add vue shim if (nuxt.options.typescript.shim) { diff --git a/packages/nuxt/src/core/templates.ts b/packages/nuxt/src/core/templates.ts index 9e120da0ff..8c54e9e79d 100644 --- a/packages/nuxt/src/core/templates.ts +++ b/packages/nuxt/src/core/templates.ts @@ -7,7 +7,7 @@ import escapeRE from 'escape-string-regexp' import { hash } from 'ohash' import { camelCase } from 'scule' import { filename } from 'pathe/utils' -import type { NuxtTemplate } from 'nuxt/schema' +import type { NuxtTemplate, NuxtTypeTemplate } from 'nuxt/schema' import { annotatePlugins, checkForCircularDependencies } from './app' @@ -96,6 +96,20 @@ export const serverPluginTemplate: NuxtTemplate = { }, } +export const appDefaults: NuxtTypeTemplate = { + filename: 'types/app-defaults.d.ts', + getContents: (ctx) => { + const isV4 = ctx.nuxt.options.future.compatibilityVersion === 4 + return ` +declare module '#app/defaults' { + type DefaultAsyncDataErrorValue = ${isV4 ? 'undefined' : 'null'} + type DefaultAsyncDataValue = ${isV4 ? 'undefined' : 'null'} + type DefaultErrorValue = ${isV4 ? 'undefined' : 'null'} + type DedupeOption = ${isV4 ? '\'cancel\' | \'defer\'' : 'boolean | \'cancel\' | \'defer\''} +}` + }, +} + export const pluginsDeclaration: NuxtTemplate = { filename: 'types/plugins.d.ts', getContents: async (ctx) => { @@ -112,8 +126,6 @@ export const pluginsDeclaration: NuxtTemplate = { const pluginsName = (await annotatePlugins(ctx.nuxt, ctx.app.plugins)).filter(p => p.name).map(p => `'${p.name}'`) - const isV4 = ctx.nuxt.options.future.compatibilityVersion === 4 - return `// Generated by Nuxt' import type { Plugin } from '#app' @@ -132,13 +144,6 @@ declare module '#app' { } } -declare module '#app/defaults' { - type DefaultAsyncDataErrorValue = ${isV4 ? 'undefined' : 'null'} - type DefaultAsyncDataValue = ${isV4 ? 'undefined' : 'null'} - type DefaultErrorValue = ${isV4 ? 'undefined' : 'null'} - type DedupeOption = ${isV4 ? '\'cancel\' | \'defer\'' : 'boolean | \'cancel\' | \'defer\''} -} - declare module 'vue' { interface ComponentCustomProperties extends NuxtAppInjections { } }