From c69961588fa7094f58347e42229fd3e849973306 Mon Sep 17 00:00:00 2001 From: PongDev Date: Tue, 3 Jun 2025 16:36:51 +0700 Subject: [PATCH] fix(type): config service get and getOrThrow methods return type When a ConfigService type is provided, the return type should match the given type; otherwise, a compile error should occur. --- lib/config.service.ts | 27 ++++++++++++++++++++------- tests/e2e/optional-generic.spec.ts | 28 ++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 7 deletions(-) diff --git a/lib/config.service.ts b/lib/config.service.ts index 2c65b150..5b052dd6 100644 --- a/lib/config.service.ts +++ b/lib/config.service.ts @@ -94,7 +94,11 @@ export class ConfigService< * @param propertyPath * @param options */ - get = any, R = PathValue>( + get< + T = K, + P extends Path = any, + R extends PathValue = PathValue, + >( propertyPath: P, options: ConfigGetOptions, ): ValidatedResult; @@ -114,7 +118,11 @@ export class ConfigService< * @param defaultValue * @param options */ - get = any, R = PathValue>( + get< + T = K, + P extends Path = any, + R extends PathValue = PathValue, + >( propertyPath: P, defaultValue: NoInferType, options: ConfigGetOptions, @@ -171,10 +179,11 @@ export class ConfigService< * @param propertyPath * @param options */ - getOrThrow = any, R = PathValue>( - propertyPath: P, - options: ConfigGetOptions, - ): Exclude; + getOrThrow< + T = K, + P extends Path = any, + R extends PathValue = PathValue, + >(propertyPath: P, options: ConfigGetOptions): Exclude; /** * Get a configuration value (either custom configuration or process environment variable) * based on property path (you can use dot notation to traverse nested object, e.g. "database.host"). @@ -196,7 +205,11 @@ export class ConfigService< * @param defaultValue * @param options */ - getOrThrow = any, R = PathValue>( + getOrThrow< + T = K, + P extends Path = any, + R extends PathValue = PathValue, + >( propertyPath: P, defaultValue: NoInferType, options: ConfigGetOptions, diff --git a/tests/e2e/optional-generic.spec.ts b/tests/e2e/optional-generic.spec.ts index ac8ae7d8..a63fd56a 100644 --- a/tests/e2e/optional-generic.spec.ts +++ b/tests/e2e/optional-generic.spec.ts @@ -69,6 +69,34 @@ describe('Optional Generic()', () => { expect(port).toEqual('default'); }); + it(`should compile error when return wrong type`, () => { + const configService = + moduleRef.get>(ConfigService); + + // @ts-expect-error: PORT is a string, not a number. Should throw an error + const port: number | undefined = configService.get('PORT', { infer: true }); + + // @ts-expect-error: PORT is a string, not a number. Should throw an error + const portWithDefaultValue: number = configService.get('PORT', '', { + infer: true, + }); + }); + + it(`should compile error when return wrong type (getOrThrow)`, () => { + const configService = + moduleRef.get>(ConfigService); + + // @ts-expect-error: PORT is a string, not a number. Should throw an error + const port: number = configService.getOrThrow('PORT', { + infer: true, + }); + + // @ts-expect-error: PORT is a string, not a number. Should throw an error + const portWithDefaultValue: number = configService.getOrThrow('PORT', '', { + infer: true, + }); + }); + afterEach(async () => { await app.close(); });