fix(schema): allow type inference of arrays in runtime config (#18931)

* fix(schema): allow type inference of arrays in runtime config

* test: add types fixture
This commit is contained in:
Daniel Roe 2023-02-10 08:36:10 +01:00 committed by GitHub
parent 9ee568efe6
commit 19bef5aba4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 8 additions and 2 deletions

View File

@ -54,6 +54,8 @@ type Overrideable<T extends Record<string, any>, Path extends string = ''> = {
? T[K] extends Record<string, any> ? T[K] extends Record<string, any>
? RuntimeValue<Overrideable<T[K], `${Path}_${UpperSnakeCase<K>}`>, `You can override this value at runtime with NUXT${Path}_${UpperSnakeCase<K>}`> ? RuntimeValue<Overrideable<T[K], `${Path}_${UpperSnakeCase<K>}`>, `You can override this value at runtime with NUXT${Path}_${UpperSnakeCase<K>}`>
: RuntimeValue<T[K], `You can override this value at runtime with NUXT${Path}_${UpperSnakeCase<K>}`> : RuntimeValue<T[K], `You can override this value at runtime with NUXT${Path}_${UpperSnakeCase<K>}`>
: K extends number
? T[K]
: never : never
} }

View File

@ -53,6 +53,7 @@ export default defineNuxtConfig({
baseAPIToken: '', baseAPIToken: '',
privateConfig: 'secret_key', privateConfig: 'secret_key',
public: { public: {
ids: [1, 2, 3],
needsFallback: undefined, needsFallback: undefined,
testConfig: 123 testConfig: 123
} }

View File

@ -133,6 +133,7 @@ describe('runtimeConfig', () => {
expectTypeOf(runtimeConfig.public.testConfig).toEqualTypeOf<number>() expectTypeOf(runtimeConfig.public.testConfig).toEqualTypeOf<number>()
expectTypeOf(runtimeConfig.public.needsFallback).toEqualTypeOf<string>() expectTypeOf(runtimeConfig.public.needsFallback).toEqualTypeOf<string>()
expectTypeOf(runtimeConfig.privateConfig).toEqualTypeOf<string>() expectTypeOf(runtimeConfig.privateConfig).toEqualTypeOf<string>()
expectTypeOf(runtimeConfig.public.ids).toEqualTypeOf<number[]>()
expectTypeOf(runtimeConfig.unknown).toEqualTypeOf<any>() expectTypeOf(runtimeConfig.unknown).toEqualTypeOf<any>()
}) })
it('provides hints on overriding these values', () => { it('provides hints on overriding these values', () => {
@ -140,7 +141,8 @@ describe('runtimeConfig', () => {
runtimeConfig: { runtimeConfig: {
public: { public: {
// @ts-expect-error // @ts-expect-error
testConfig: 'test' testConfig: 'test',
ids: [1, 2]
} }
} }
}) })
@ -148,6 +150,7 @@ describe('runtimeConfig', () => {
expectTypeOf(val.runtimeConfig!.privateConfig).toEqualTypeOf<undefined | RuntimeValue<string, 'You can override this value at runtime with NUXT_PRIVATE_CONFIG'>>() expectTypeOf(val.runtimeConfig!.privateConfig).toEqualTypeOf<undefined | RuntimeValue<string, 'You can override this value at runtime with NUXT_PRIVATE_CONFIG'>>()
expectTypeOf(val.runtimeConfig!.baseURL).toEqualTypeOf<undefined | RuntimeValue<string, 'You can override this value at runtime with NUXT_BASE_URL'>>() expectTypeOf(val.runtimeConfig!.baseURL).toEqualTypeOf<undefined | RuntimeValue<string, 'You can override this value at runtime with NUXT_BASE_URL'>>()
expectTypeOf(val.runtimeConfig!.baseAPIToken).toEqualTypeOf<undefined | RuntimeValue<string, 'You can override this value at runtime with NUXT_BASE_API_TOKEN'>>() expectTypeOf(val.runtimeConfig!.baseAPIToken).toEqualTypeOf<undefined | RuntimeValue<string, 'You can override this value at runtime with NUXT_BASE_API_TOKEN'>>()
expectTypeOf(val.runtimeConfig!.public!.ids).toEqualTypeOf<undefined | RuntimeValue<Array<number | undefined>, 'You can override this value at runtime with NUXT_PUBLIC_IDS'>>()
expectTypeOf(val.runtimeConfig!.unknown).toEqualTypeOf<any>() expectTypeOf(val.runtimeConfig!.unknown).toEqualTypeOf<any>()
}) })
}) })