Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 20 additions & 7 deletions lib/config.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,11 @@ export class ConfigService<
* @param propertyPath
* @param options
*/
get<T = K, P extends Path<T> = any, R = PathValue<T, P>>(
get<
T = K,
P extends Path<T> = any,
R extends PathValue<T, P> = PathValue<T, P>,
>(
propertyPath: P,
options: ConfigGetOptions,
): ValidatedResult<WasValidated, R>;
Expand All @@ -114,7 +118,11 @@ export class ConfigService<
* @param defaultValue
* @param options
*/
get<T = K, P extends Path<T> = any, R = PathValue<T, P>>(
get<
T = K,
P extends Path<T> = any,
R extends PathValue<T, P> = PathValue<T, P>,
>(
propertyPath: P,
defaultValue: NoInferType<R>,
options: ConfigGetOptions,
Expand Down Expand Up @@ -171,10 +179,11 @@ export class ConfigService<
* @param propertyPath
* @param options
*/
getOrThrow<T = K, P extends Path<T> = any, R = PathValue<T, P>>(
propertyPath: P,
options: ConfigGetOptions,
): Exclude<R, undefined>;
getOrThrow<
T = K,
P extends Path<T> = any,
R extends PathValue<T, P> = PathValue<T, P>,
>(propertyPath: P, options: ConfigGetOptions): Exclude<R, undefined>;
/**
* 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").
Expand All @@ -196,7 +205,11 @@ export class ConfigService<
* @param defaultValue
* @param options
*/
getOrThrow<T = K, P extends Path<T> = any, R = PathValue<T, P>>(
getOrThrow<
T = K,
P extends Path<T> = any,
R extends PathValue<T, P> = PathValue<T, P>,
>(
propertyPath: P,
defaultValue: NoInferType<R>,
options: ConfigGetOptions,
Expand Down
28 changes: 28 additions & 0 deletions tests/e2e/optional-generic.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,34 @@ describe('Optional Generic()', () => {
expect(port).toEqual('default');
});

it(`should compile error when return wrong type`, () => {
const configService =
moduleRef.get<ConfigService<{ PORT: string }>>(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<{ PORT: string }>>(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();
});
Expand Down