mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-22 05:35:13 +00:00
fix(nuxt,schema): merge custom and resolved app configs (#19602)
This commit is contained in:
parent
3684de58f4
commit
60b4c48eb4
@ -142,17 +142,18 @@ export default defineNuxtModule({
|
||||
_types +
|
||||
`
|
||||
export type CustomAppConfig = Exclude<NuxtCustomSchema['appConfig'], undefined>
|
||||
type _CustomAppConfig = CustomAppConfig
|
||||
|
||||
declare module '@nuxt/schema' {
|
||||
interface NuxtConfig extends NuxtCustomSchema {}
|
||||
interface NuxtOptions extends NuxtCustomSchema {}
|
||||
interface CustomAppConfig extends CustomAppConfig {}
|
||||
interface NuxtConfig extends Omit<NuxtCustomSchema, 'appConfig'> {}
|
||||
interface NuxtOptions extends Omit<NuxtCustomSchema, 'appConfig'> {}
|
||||
interface CustomAppConfig extends _CustomAppConfig {}
|
||||
}
|
||||
|
||||
declare module 'nuxt/schema' {
|
||||
interface NuxtConfig extends NuxtCustomSchema {}
|
||||
interface NuxtOptions extends NuxtCustomSchema {}
|
||||
interface CustomAppConfig extends CustomAppConfig {}
|
||||
interface NuxtConfig extends Omit<NuxtCustomSchema, 'appConfig'> {}
|
||||
interface NuxtOptions extends Omit<NuxtCustomSchema, 'appConfig'> {}
|
||||
interface CustomAppConfig extends _CustomAppConfig {}
|
||||
}
|
||||
`
|
||||
const typesPath = resolve(
|
||||
|
@ -194,14 +194,28 @@ export const appConfigDeclarationTemplate: NuxtTemplate = {
|
||||
filename: 'types/app.config.d.ts',
|
||||
getContents: ({ app, nuxt }) => {
|
||||
return `
|
||||
import type { CustomAppConfig } from 'nuxt/schema'
|
||||
import type { Defu } from 'defu'
|
||||
${app.configs.map((id: string, index: number) => `import ${`cfg${index}`} from ${JSON.stringify(id.replace(/(?<=\w)\.\w+$/g, ''))}`).join('\n')}
|
||||
|
||||
declare const inlineConfig = ${JSON.stringify(nuxt.options.appConfig, null, 2)}
|
||||
type ResolvedAppConfig = Defu<typeof inlineConfig, [${app.configs.map((_id: string, index: number) => `typeof cfg${index}`).join(', ')}]>
|
||||
|
||||
type MergedAppConfig<Resolved extends Record<string, any>, Custom extends Record<string, any>> = {
|
||||
[K in keyof Resolved]: K extends keyof Custom
|
||||
? Custom[K] extends Record<string, any>
|
||||
? Resolved[K] extends Record<string, any>
|
||||
? MergedAppConfig<Resolved[K], Custom[K]>
|
||||
: Exclude<Custom[K], undefined>
|
||||
: Exclude<Custom[K], undefined>
|
||||
: Resolved[K]
|
||||
}
|
||||
|
||||
declare module 'nuxt/schema' {
|
||||
interface AppConfig extends ResolvedAppConfig { }
|
||||
interface AppConfig extends MergedAppConfig<ResolvedAppConfig, CustomAppConfig> { }
|
||||
}
|
||||
declare module '@nuxt/schema' {
|
||||
interface AppConfig extends MergedAppConfig<ResolvedAppConfig, CustomAppConfig> { }
|
||||
}
|
||||
`
|
||||
}
|
||||
|
@ -136,7 +136,7 @@ export interface RuntimeConfig extends RuntimeConfigNamespace {
|
||||
|
||||
// -- App Config --
|
||||
|
||||
export interface CustomAppConfig extends Record<string, any> { }
|
||||
export interface CustomAppConfig { }
|
||||
|
||||
export interface AppConfigInput extends CustomAppConfig {
|
||||
/** @deprecated reserved */
|
||||
@ -156,4 +156,4 @@ export interface NuxtAppConfig {
|
||||
keepalive: boolean | KeepAliveProps
|
||||
}
|
||||
|
||||
export interface AppConfig extends CustomAppConfig { }
|
||||
export interface AppConfig { }
|
||||
|
6
test/fixtures/basic/nuxt.schema.ts
vendored
6
test/fixtures/basic/nuxt.schema.ts
vendored
@ -1,6 +1,10 @@
|
||||
export default defineNuxtSchema({
|
||||
appConfig: {
|
||||
/** This is an example app config defined in custom schema */
|
||||
/**
|
||||
* This is an example app config defined in custom schema
|
||||
*
|
||||
* @type {123 | 456}
|
||||
*/
|
||||
userConfig: 123
|
||||
}
|
||||
})
|
||||
|
9
test/fixtures/basic/types.ts
vendored
9
test/fixtures/basic/types.ts
vendored
@ -246,13 +246,12 @@ describe('composables', () => {
|
||||
describe('app config', () => {
|
||||
it('merges app config as expected', () => {
|
||||
interface ExpectedMergedAppConfig {
|
||||
fromLayer: boolean,
|
||||
fromNuxtConfig: boolean,
|
||||
fromLayer: boolean
|
||||
fromNuxtConfig: boolean
|
||||
nested: {
|
||||
val: number
|
||||
},
|
||||
userConfig: number
|
||||
[key: string]: any
|
||||
}
|
||||
userConfig: 123 | 456
|
||||
}
|
||||
expectTypeOf<AppConfig>().toEqualTypeOf<ExpectedMergedAppConfig>()
|
||||
})
|
||||
|
Loading…
Reference in New Issue
Block a user