Skip to content

Commit

Permalink
fix(): property path is not detected issue #752
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilmysliwiec committed Nov 26, 2021
1 parent b615191 commit 8d853c0
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 11 deletions.
2 changes: 1 addition & 1 deletion lib/config.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export interface ConfigGetOptions {
infer: true;
}

type KeyOf<T> = T extends unknown ? string : keyof T;
type KeyOf<T> = keyof T extends never ? string : keyof T;

@Injectable()
export class ConfigService<
Expand Down
4 changes: 1 addition & 3 deletions lib/types/no-infer.type.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
export type NoInferType<T> = T extends unknown
? any
: [T][T extends any ? 0 : never];
export type NoInferType<T> = [T][T extends any ? 0 : never];
4 changes: 1 addition & 3 deletions lib/types/path-value.type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ export type PathImpl<T, Key extends keyof T> = Key extends string
: never;

export type PathImpl2<T> = PathImpl<T, keyof T> | keyof T;
export type Path<T> = T extends unknown
? any
: PathImpl2<T> extends string | keyof T
export type Path<T> = PathImpl2<T> extends string | keyof T
? PathImpl2<T>
: keyof T;

Expand Down
10 changes: 6 additions & 4 deletions tests/e2e/optional-generic.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,17 @@ describe('Optional Generic()', () => {

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

Expand Down
1 change: 1 addition & 0 deletions tests/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export class AppModule {
imports: [
ConfigModule.forRoot({
envFilePath: join(__dirname, '.env'),
load: [() => ({ obj: { test: 'true', test2: undefined } })],
}),
],
};
Expand Down

0 comments on commit 8d853c0

Please sign in to comment.