Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(nuxt): improved typing support for app config #20526

Merged
merged 1 commit into from
Apr 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 10 additions & 8 deletions packages/nuxt/src/core/templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,15 +222,17 @@ 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 IsAny<T> = 0 extends 1 & T ? true : false

type MergedAppConfig<Resolved extends Record<string, any>, Custom extends Record<string, any>> = {
[K in keyof Resolved]: K extends keyof Custom
? IsAny<Custom[K]> extends true
type MergedAppConfig<Resolved extends Record<string, unknown>, Custom extends Record<string, unknown>> = {
[K in keyof (Resolved & Custom)]: K extends keyof Custom
? unknown extends Custom[K]
? Resolved[K]
: 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>
: IsAny<Custom[K]> extends true
? Resolved[K]
: 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]
}

Expand Down
8 changes: 6 additions & 2 deletions packages/schema/src/types/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,9 @@ export interface RuntimeConfig extends RuntimeConfigNamespace {

// -- App Config --

export interface CustomAppConfig { }
export interface CustomAppConfig {
[key: string]: unknown
}

export interface AppConfigInput extends CustomAppConfig {
/** @deprecated reserved */
Expand All @@ -158,4 +160,6 @@ export interface NuxtAppConfig {
keepalive: boolean | KeepAliveProps
}

export interface AppConfig { }
export interface AppConfig {
[key: string]: unknown
}
14 changes: 14 additions & 0 deletions test/fixtures/basic/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,20 @@ export default defineNuxtConfig({
},
telemetry: false, // for testing telemetry types - it is auto-disabled in tests
hooks: {
'schema:extend' (schemas) {
schemas.push({
appConfig: {
someThing: {
value: {
$default: 'default',
$schema: {
tsType: 'string | false'
}
}
}
}
})
},
'prepare:types' ({ tsConfig }) {
tsConfig.include = tsConfig.include!.filter(i => i !== '../../../../**/*')
},
Expand Down
4 changes: 4 additions & 0 deletions test/fixtures/basic/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,10 @@ describe('app config', () => {
val: number
}
userConfig: 123 | 456
someThing?: {
value?: string | false,
}
[key: string]: unknown
}
expectTypeOf<AppConfig>().toEqualTypeOf<ExpectedMergedAppConfig>()
})
Expand Down