mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-25 15:15:19 +00:00
fix(nuxt): improved typing support for app config (#20526)
This commit is contained in:
parent
a21a520736
commit
dd0d13d425
@ -222,9 +222,11 @@ 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 ResolvedAppConfig = Defu<typeof inlineConfig, [${app.configs.map((_id: string, index: number) => `typeof cfg${index}`).join(', ')}]>
|
||||||
type IsAny<T> = 0 extends 1 & T ? true : false
|
type IsAny<T> = 0 extends 1 & T ? true : false
|
||||||
|
|
||||||
type MergedAppConfig<Resolved extends Record<string, any>, Custom extends Record<string, any>> = {
|
type MergedAppConfig<Resolved extends Record<string, unknown>, Custom extends Record<string, unknown>> = {
|
||||||
[K in keyof Resolved]: K extends keyof Custom
|
[K in keyof (Resolved & Custom)]: K extends keyof Custom
|
||||||
? IsAny<Custom[K]> extends true
|
? unknown extends Custom[K]
|
||||||
|
? Resolved[K]
|
||||||
|
: IsAny<Custom[K]> extends true
|
||||||
? Resolved[K]
|
? Resolved[K]
|
||||||
: Custom[K] extends Record<string, any>
|
: Custom[K] extends Record<string, any>
|
||||||
? Resolved[K] extends Record<string, any>
|
? Resolved[K] extends Record<string, any>
|
||||||
|
@ -138,7 +138,9 @@ export interface RuntimeConfig extends RuntimeConfigNamespace {
|
|||||||
|
|
||||||
// -- App Config --
|
// -- App Config --
|
||||||
|
|
||||||
export interface CustomAppConfig { }
|
export interface CustomAppConfig {
|
||||||
|
[key: string]: unknown
|
||||||
|
}
|
||||||
|
|
||||||
export interface AppConfigInput extends CustomAppConfig {
|
export interface AppConfigInput extends CustomAppConfig {
|
||||||
/** @deprecated reserved */
|
/** @deprecated reserved */
|
||||||
@ -158,4 +160,6 @@ export interface NuxtAppConfig {
|
|||||||
keepalive: boolean | KeepAliveProps
|
keepalive: boolean | KeepAliveProps
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface AppConfig { }
|
export interface AppConfig {
|
||||||
|
[key: string]: unknown
|
||||||
|
}
|
||||||
|
14
test/fixtures/basic/nuxt.config.ts
vendored
14
test/fixtures/basic/nuxt.config.ts
vendored
@ -145,6 +145,20 @@ export default defineNuxtConfig({
|
|||||||
},
|
},
|
||||||
telemetry: false, // for testing telemetry types - it is auto-disabled in tests
|
telemetry: false, // for testing telemetry types - it is auto-disabled in tests
|
||||||
hooks: {
|
hooks: {
|
||||||
|
'schema:extend' (schemas) {
|
||||||
|
schemas.push({
|
||||||
|
appConfig: {
|
||||||
|
someThing: {
|
||||||
|
value: {
|
||||||
|
$default: 'default',
|
||||||
|
$schema: {
|
||||||
|
tsType: 'string | false'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
},
|
||||||
'prepare:types' ({ tsConfig }) {
|
'prepare:types' ({ tsConfig }) {
|
||||||
tsConfig.include = tsConfig.include!.filter(i => i !== '../../../../**/*')
|
tsConfig.include = tsConfig.include!.filter(i => i !== '../../../../**/*')
|
||||||
},
|
},
|
||||||
|
4
test/fixtures/basic/types.ts
vendored
4
test/fixtures/basic/types.ts
vendored
@ -295,6 +295,10 @@ describe('app config', () => {
|
|||||||
val: number
|
val: number
|
||||||
}
|
}
|
||||||
userConfig: 123 | 456
|
userConfig: 123 | 456
|
||||||
|
someThing?: {
|
||||||
|
value?: string | false,
|
||||||
|
}
|
||||||
|
[key: string]: unknown
|
||||||
}
|
}
|
||||||
expectTypeOf<AppConfig>().toEqualTypeOf<ExpectedMergedAppConfig>()
|
expectTypeOf<AppConfig>().toEqualTypeOf<ExpectedMergedAppConfig>()
|
||||||
})
|
})
|
||||||
|
Loading…
Reference in New Issue
Block a user