Skip to content

Commit 4b02314

Browse files
authored
fix(types): broken nuxt v4 check (#300)
1 parent b26b301 commit 4b02314

2 files changed

Lines changed: 22 additions & 7 deletions

File tree

src/templates.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
import type { Nuxt } from '@nuxt/schema'
22
import type { ModuleOptions } from './module'
3-
import { addTypeTemplate } from '@nuxt/kit'
3+
import { addTypeTemplate, isNuxtMajorVersion } from '@nuxt/kit'
44

55
interface TemplateContext {
66
nuxt: Nuxt
77
config: ModuleOptions
88
}
99

1010
export function registerTypeTemplates({ nuxt }: TemplateContext) {
11-
// Nuxt 4 augments both 'nitropack' and 'nitropack/types' (matching @nuxt/nitro-server).
12-
// Augmenting only one leaves the other's re-exported NitroRouteConfig/NitroRouteRules
13-
// without our `robots` key, which breaks consumers like @nuxt/kit's extendRouteRules
14-
// that pull the type through both module paths.
15-
const isNuxt4 = Number(nuxt.options.future?.compatibilityVersion) === 4
16-
const nitroModules = isNuxt4 ? ['nitropack', 'nitropack/types'] : ['nitropack']
11+
// Nuxt 4 ships Nitro v3, which exposes `nitropack/types` as a public subpath.
12+
// @nuxt/kit's extendRouteRules unions NitroRouteConfig across both 'nitropack'
13+
// and 'nitropack/types', so augmenting only one leaves the other's re-exported
14+
// interface without our `robots` key.
15+
const nitroModules = isNuxtMajorVersion(4, nuxt)
16+
? ['nitropack', 'nitropack/types']
17+
: ['nitropack']
1718

1819
// Nuxt-only type augmentations (PageMeta)
1920
addTypeTemplate({

test/types/templates.test-d.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import type {
77
import type { H3EventContext } from 'h3'
88
import type { NitroRouteConfig as NitroRouteConfigPack, NitroRouteRules as NitroRouteRulesPack } from 'nitropack'
99
import type { NitroRouteConfig, NitroRouteRules, NitroRuntimeHooks } from 'nitropack/types'
10+
import type { NuxtConfig } from 'nuxt/schema'
1011
import type { PageMeta } from '#app'
1112
import { extendRouteRules } from '@nuxt/kit'
1213
import { describe, expectTypeOf, it } from 'vitest'
@@ -65,6 +66,19 @@ describe('nitropack augmentations (cross-module-path)', () => {
6566
})
6667
})
6768

69+
// Regression test for https://github.com/nuxt-modules/robots/issues/299
70+
// `nuxt.config.ts` routeRules are typed via `NuxtConfig['routeRules']`,
71+
// which (in Nuxt 4) resolves NitroRouteConfig through 'nitropack/types'.
72+
// If only 'nitropack' is augmented, declaring `robots` in routeRules fails
73+
// with TS2353 "Object literal may only specify known properties".
74+
describe('NuxtConfig routeRules (issue #299)', () => {
75+
it('NuxtConfig.routeRules accepts a robots-tagged literal', () => {
76+
expectTypeOf<{ '/admin': { robots: false } }>().toExtend<NonNullable<NuxtConfig['routeRules']>>()
77+
expectTypeOf<{ '/private': { robots: 'noindex, nofollow' } }>().toExtend<NonNullable<NuxtConfig['routeRules']>>()
78+
expectTypeOf<{ '/legacy': { robots: { indexable: false, rule: 'noindex' } } }>().toExtend<NonNullable<NuxtConfig['routeRules']>>()
79+
})
80+
})
81+
6882
describe('h3 augmentations', () => {
6983
it('H3EventContext.robots is RobotsContext', () => {
7084
expectTypeOf<H3EventContext['robots']>().toEqualTypeOf<RobotsContext>()

0 commit comments

Comments
 (0)