Skip to content

Commit

Permalink
fix(): do not remove undefined from ConfigService.get return type
Browse files Browse the repository at this point in the history
The return type now no longer excludes undefined if `WasValidated` is
true. Previously, optional properties were stripped of `undefined`
which hid possible null errors.

BREAKING CHANGE: Users that have strictNullChecks enabled, have
  optional properties on their Env class, and depend on the behavior
  of ConfigService.get stripping undefined from the return type will
  receive type errors.
  • Loading branch information
MatthiasKunnen committed Apr 30, 2023
1 parent f1abd2d commit 89cce99
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions lib/config.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ import {
import { NoInferType, Path, PathValue } from './types';

/**
* `ExcludeUndefinedIf<ExcludeUndefined, T>
* `ValidatedResult<WasValidated, T>
*
* If `ExcludeUndefined` is `true`, remove `undefined` from `T`.
* If `WasValidated` is `true`, return `T`.
* Otherwise, constructs the type `T` with `undefined`.
*/
type ExcludeUndefinedIf<
ExcludeUndefined extends boolean,
type ValidatedResult<
WasValidated extends boolean,
T,
> = ExcludeUndefined extends true ? Exclude<T, undefined> : T | undefined;
> = WasValidated extends true ? T : T | undefined;

export interface ConfigGetOptions {
/**
Expand Down Expand Up @@ -56,7 +56,7 @@ export class ConfigService<
* based on property path (you can use dot notation to traverse nested object, e.g. "database.host").
* @param propertyPath
*/
get<T = any>(propertyPath: KeyOf<K>): ExcludeUndefinedIf<WasValidated, T>;
get<T = any>(propertyPath: KeyOf<K>): ValidatedResult<WasValidated, T>;
/**
* 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 @@ -66,7 +66,7 @@ export class ConfigService<
get<T = K, P extends Path<T> = any, R = PathValue<T, P>>(
propertyPath: P,
options: ConfigGetOptions,
): ExcludeUndefinedIf<WasValidated, R>;
): ValidatedResult<WasValidated, R>;
/**
* 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

0 comments on commit 89cce99

Please sign in to comment.