Skip to content

Commit

Permalink
Merge pull request #1027 from swarthy/fix/getOrThrow-inter-return-value
Browse files Browse the repository at this point in the history
fix(common): infer getOrThrow return value
  • Loading branch information
kamilmysliwiec committed Sep 29, 2022
2 parents 1cfca39 + 7307492 commit f053a4f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/config.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,10 @@ export class ConfigService<
* @param propertyPath
* @param options
*/
getOrThrow<T = K, P extends Path<T> = any>(
getOrThrow<T = K, P extends Path<T> = any, R = PathValue<T, P>>(
propertyPath: P,
options: ConfigGetOptions,
): Exclude<T, undefined>;
): 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 Down
16 changes: 16 additions & 0 deletions tests/e2e/optional-generic.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,22 @@ describe('Optional Generic()', () => {
expect(testWithDefaultValue).toBeTruthy();
});

it(`should infer type from a dot notation (getOrThrow)`, () => {
const configService =
moduleRef.get<ConfigService<{ obj: { test: boolean; test2?: boolean } }>>(
ConfigService,
);

const obj = configService.getOrThrow('obj', { infer: true });
const test = configService.getOrThrow('obj.test', { infer: true });
const testWithDefaultValue = configService.getOrThrow('obj.test2', true, {
infer: true,
});
expect(obj?.test).toEqual('true');
expect(test).toEqual('true');
expect(testWithDefaultValue).toBeTruthy();
});

it(`should allow any key without a generic`, () => {
const configService = moduleRef.get<ConfigService>(ConfigService);
const port = configService.get('PORT');
Expand Down

0 comments on commit f053a4f

Please sign in to comment.