From 8d853c0c6ec07f70332b34bb96181bb0a624fe9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20My=C5=9Bliwiec?= Date: Fri, 26 Nov 2021 08:51:23 +0100 Subject: [PATCH] fix(): property path is not detected issue #752 --- lib/config.service.ts | 2 +- lib/types/no-infer.type.ts | 4 +--- lib/types/path-value.type.ts | 4 +--- tests/e2e/optional-generic.spec.ts | 10 ++++++---- tests/src/app.module.ts | 1 + 5 files changed, 10 insertions(+), 11 deletions(-) diff --git a/lib/config.service.ts b/lib/config.service.ts index 78870942..598ad52d 100644 --- a/lib/config.service.ts +++ b/lib/config.service.ts @@ -29,7 +29,7 @@ export interface ConfigGetOptions { infer: true; } -type KeyOf = T extends unknown ? string : keyof T; +type KeyOf = keyof T extends never ? string : keyof T; @Injectable() export class ConfigService< diff --git a/lib/types/no-infer.type.ts b/lib/types/no-infer.type.ts index 9c206690..6bfb3b61 100644 --- a/lib/types/no-infer.type.ts +++ b/lib/types/no-infer.type.ts @@ -1,3 +1 @@ -export type NoInferType = T extends unknown - ? any - : [T][T extends any ? 0 : never]; +export type NoInferType = [T][T extends any ? 0 : never]; diff --git a/lib/types/path-value.type.ts b/lib/types/path-value.type.ts index 744d6cef..9cc8cef1 100644 --- a/lib/types/path-value.type.ts +++ b/lib/types/path-value.type.ts @@ -8,9 +8,7 @@ export type PathImpl = Key extends string : never; export type PathImpl2 = PathImpl | keyof T; -export type Path = T extends unknown - ? any - : PathImpl2 extends string | keyof T +export type Path = PathImpl2 extends string | keyof T ? PathImpl2 : keyof T; diff --git a/tests/e2e/optional-generic.spec.ts b/tests/e2e/optional-generic.spec.ts index 4e0f886e..bd9b153e 100644 --- a/tests/e2e/optional-generic.spec.ts +++ b/tests/e2e/optional-generic.spec.ts @@ -25,15 +25,17 @@ describe('Optional Generic()', () => { it(`should infer type from a dot notation`, () => { const configService = - moduleRef.get>(ConfigService); + moduleRef.get>( + ConfigService, + ); const obj = configService.get('obj', { infer: true }); const test = configService.get('obj.test', { infer: true }); - const testWithDefaultValue = configService.get('obj.test', true, { + const testWithDefaultValue = configService.get('obj.test2', true, { infer: true, }); - expect(obj?.test).toBeUndefined(); - expect(test).toBeUndefined(); + expect(obj?.test).toEqual('true'); + expect(test).toEqual('true'); expect(testWithDefaultValue).toBeTruthy(); }); diff --git a/tests/src/app.module.ts b/tests/src/app.module.ts index e25cc2d6..502b2fbe 100644 --- a/tests/src/app.module.ts +++ b/tests/src/app.module.ts @@ -74,6 +74,7 @@ export class AppModule { imports: [ ConfigModule.forRoot({ envFilePath: join(__dirname, '.env'), + load: [() => ({ obj: { test: 'true', test2: undefined } })], }), ], };