Skip to content

Commit

Permalink
fix(): revert extends type constraint from config service
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilmysliwiec committed Nov 22, 2021
1 parent ed4b2fe commit 776d467
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/config.service.ts
Expand Up @@ -31,7 +31,7 @@ export interface ConfigGetOptions {

@Injectable()
export class ConfigService<
K extends Record<string, unknown> = Record<string, unknown>,
K = Record<string, unknown>,
WasValidated extends boolean = false,
> {
private set isCacheEnabled(value: boolean) {
Expand Down
14 changes: 12 additions & 2 deletions tests/src/app.module.ts
Expand Up @@ -9,16 +9,25 @@ import nestedDatabaseConfig from './nested-database.config';

type Config = {
database: ConfigType<typeof databaseConfig> & {
driver: ConfigType<typeof nestedDatabaseConfig>
driver: ConfigType<typeof nestedDatabaseConfig>;
};
};

interface ConfigTypeAsInterface {
database: ConfigType<typeof databaseConfig> & {
driver: ConfigType<typeof nestedDatabaseConfig>;
};
}
@Module({})
export class AppModule {
constructor(
private readonly configService: ConfigService,
// The following is the same object as above but narrowing its types
private readonly configServiceNarrowed: ConfigService<Config, true>,
private readonly configServiceNarrowed2: ConfigService<
ConfigTypeAsInterface,
true
>,
@Optional()
@Inject(databaseConfig.KEY)
private readonly dbConfig: ConfigType<typeof databaseConfig>,
Expand All @@ -35,7 +44,8 @@ export class AppModule {
const identityString = (v: string) => v;
const identityNumber = (v: number) => v;
// Act
const knowConfig = this.configServiceNarrowed.get<Config['database']>('database');
const knowConfig =
this.configServiceNarrowed.get<Config['database']>('database');
// Assert
// We don't need type assertions bellow anymore since `knowConfig` is not
// expected to be `undefined` beforehand.
Expand Down

0 comments on commit 776d467

Please sign in to comment.