fix(nuxt): add types for globally injected $config object (#20081)

This commit is contained in:
Daniel Roe 2023-04-04 14:34:39 +01:00 committed by GitHub
parent e7623ec0b7
commit b811742f89
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 2 deletions

View File

@ -130,7 +130,7 @@ export const schemaTemplate: NuxtTemplate<TemplateContext> = {
const modules = moduleInfo.map(meta => [genString(meta.configKey), getImportName(meta.importName)])
return [
"import { NuxtModule } from 'nuxt/schema'",
"import { NuxtModule, RuntimeConfig } from 'nuxt/schema'",
"declare module 'nuxt/schema' {",
' interface NuxtConfig {',
...modules.map(([configKey, importName]) =>
@ -154,7 +154,18 @@ export const schemaTemplate: NuxtTemplate<TemplateContext> = {
allowExtraKeys: false,
indentation: 2
}),
'}'
'}',
`declare module 'vue' {
interface ComponentCustomProperties {
$config: RuntimeConfig
}
}`,
// TODO: remove when webstorm has support for augumenting 'vue' directly
`declare module '@vue/runtime-dom' {
interface ComponentCustomProperties {
$config: RuntimeConfig
}
}`
].join('\n')
}
}

View File

@ -146,6 +146,13 @@ describe('runtimeConfig', () => {
expectTypeOf(runtimeConfig.privateConfig).toEqualTypeOf<string>()
expectTypeOf(runtimeConfig.public.ids).toEqualTypeOf<number[]>()
expectTypeOf(runtimeConfig.unknown).toEqualTypeOf<any>()
const injectedConfig = useNuxtApp().$config
expectTypeOf(injectedConfig.public.testConfig).toEqualTypeOf<number>()
expectTypeOf(injectedConfig.public.needsFallback).toEqualTypeOf<string>()
expectTypeOf(injectedConfig.privateConfig).toEqualTypeOf<string>()
expectTypeOf(injectedConfig.public.ids).toEqualTypeOf<number[]>()
expectTypeOf(injectedConfig.unknown).toEqualTypeOf<any>()
})
it('provides hints on overriding these values', () => {
const val = defineNuxtConfig({