fix(nuxt): provide types for modules as array (#18416)

Co-authored-by: Daniel Roe <daniel@roe.dev>
This commit is contained in:
Inesh Bose 2023-01-21 16:55:44 +00:00 committed by GitHub
parent 585e8be2e0
commit 98d292ae85
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 17 additions and 4 deletions

View File

@ -121,14 +121,17 @@ export const schemaTemplate: NuxtTemplate<TemplateContext> = {
})).filter(m => m.configKey && m.name && !adHocModules.includes(m.name))
const relativeRoot = relative(resolve(nuxt.options.buildDir, 'types'), nuxt.options.rootDir)
const getImportName = (name: string) => (name.startsWith('.') ? './' + join(relativeRoot, name) : name).replace(/\.\w+$/, '')
const modules = moduleInfo.map(meta => [genString(meta.configKey), getImportName(meta.importName)])
return [
"import { NuxtModule } from '@nuxt/schema'",
"declare module '@nuxt/schema' {",
' interface NuxtConfig {',
...moduleInfo.filter(Boolean).map(meta =>
` [${genString(meta.configKey)}]?: typeof ${genDynamicImport(meta.importName.startsWith('.') ? './' + join(relativeRoot, meta.importName) : meta.importName, { wrapper: false })}.default extends NuxtModule<infer O> ? Partial<O> : Record<string, any>`
...modules.map(([configKey, importName]) =>
` [${configKey}]?: typeof ${genDynamicImport(importName, { wrapper: false })}.default extends NuxtModule<infer O> ? Partial<O> : Record<string, any>`
),
modules.length > 0 ? ` modules?: (NuxtModule | string | [NuxtModule | string, Record<string, any>] | ${modules.map(([configKey, importName]) => `[${genString(importName)}, NuxtConfig[${configKey}]]`).join(' | ')})[],` : '',
' }',
generateTypes(await resolveSchema(Object.fromEntries(Object.entries(nuxt.options.runtimeConfig).filter(([key]) => key !== 'public'))),
{

View File

@ -3,7 +3,8 @@ import { defineNuxtModule, addPlugin, useNuxt } from '@nuxt/kit'
export default defineNuxtModule({
defaults: {
enabled: true
enabled: true,
typeTest: (value: boolean) => typeof value === 'boolean'
},
meta: {
name: 'my-module',

View File

@ -55,7 +55,16 @@ export default defineNuxtConfig({
}
},
modules: [
[
'~/modules/example',
{
typeTest (val) {
// @ts-expect-error module type defines val as boolean
const b: string = val
return !!b
}
}
],
function (_, nuxt) {
if (process.env.TEST_WITH_WEBPACK) { return }