You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[ ] Regression
[ ] Bug report
[x] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.
Current behavior
Regardless if we set up an schema validation or not, the type of ConfigService#get is undefined (when strictNullChecks:true from TSC) because ConfigService doesn't know about ConfigModule#forRoot usage. Thus we end up using the non-null assertion operator or type assertions whenever we use ConfigService#get
@Injectable()exportclassAppService{constructor(privatereadonlyconfigService: ConfigService<{foo: string,a: {b:number}}>){}getFoo(): string{consta=this.configService.get('a')constb: number=a!.b// ^ This could be annoying if we type it many times!!returnthis.configService.get('foo')!// ^}}
Expected behavior
We can tell to ConfigService that the validation was properly set up, then ConfigService#get cannot return undefined when we pass some valid prop
No more type assertions!
getFoo(): string {consta=this.configService.get('foo')constb: number=a.bthis.configService.get('bar')// TSC ERROR, as usualreturnthis.configService.get('foo')}
What is the motivation / use case for changing the behavior?
Improve developer experience.
Possible solution
Add another (optional) type arg to ConfigService generics:
@Injectable()exportclassAppService{constructor(privatereadonlyconfigService: ConfigService<{foo: string},true>){}// ^^^^ Telling to ConfigService// that everything was validated}
I prototyped this one here:
config.service.d.ts we become something like
import{NoInferType,Path,PathValue}from'./types';exportinterfaceConfigGetOptions{infer: true;}/** * `WithUndefined<HasValidation, T>` * * Evaluates to `T` if `HasValidation` is `true`. Otherwise, `T | undefined` */typeWithUndefined<HasValidationextendsboolean,T>=HasValidationextendstrue ? T : T|undefinedexportdeclareclassConfigService<K=Record<string,unknown>,HasValidationextendsboolean=false>{privatereadonlyinternalConfig;privatesetisCacheEnabled(value);privategetisCacheEnabled();privatereadonlycache;private_isCacheEnabled;constructor(internalConfig?: Record<string,any>);// get<T = any>(propertyPath: keyof K): T | undefined;get<T=any>(propertyPath: keyofK): WithUndefined<HasValidation,T>;// get<T = K, P extends Path<T> = any, R = PathValue<T, P>>(propertyPath: P, options: ConfigGetOptions): R | undefined;get<T=K,PextendsPath<T>=any,R=PathValue<T,P>>(propertyPath: P,options: ConfigGetOptions): WithUndefined<HasValidation,R>;get<T=any>(propertyPath: keyofK,defaultValue: NoInferType<T>): T;// get<T = K, P extends Path<T> = any, R = PathValue<T, P>>(propertyPath: P, defaultValue: NoInferType<R>, options: ConfigGetOptions): R | undefined;get<T=K,PextendsPath<T>=any,R=PathValue<T,P>>(propertyPath: P,defaultValue: NoInferType<R>,options: ConfigGetOptions): WithUndefined<HasValidation,R>;privategetFromCache;privategetFromValidatedEnv;privategetFromProcessEnv;privategetFromInternalConfig;privatesetInCacheIfDefined;privateisGetOptionsObject;}
Environment
Nest version: ^8
@nestjs/config: ^1
typescript: ^4.4
For Tooling issues:
- Node version: XX
- Platform: Linux
Others:
The text was updated successfully, but these errors were encountered:
micalevisk
changed the title
Let ConfigService know about the validation defined in ConfigRoot to avoid type assertions
Let ConfigService know about the validation defined in ConfigModule#forRoot to avoid type assertions
Sep 12, 2021
I'm submitting a...
Current behavior
Regardless if we set up an schema validation or not, the type of
ConfigService#get
isundefined
(whenstrictNullChecks:true
from TSC) becauseConfigService
doesn't know aboutConfigModule#forRoot
usage. Thus we end up using the non-null assertion operator or type assertions whenever we useConfigService#get
Expected behavior
We can tell to
ConfigService
that the validation was properly set up, thenConfigService#get
cannot returnundefined
when we pass some valid propNo more type assertions!
What is the motivation / use case for changing the behavior?
Improve developer experience.
Possible solution
Add another (optional) type arg to
ConfigService
generics:I prototyped this one here:
config.service.d.ts
we become something likeEnvironment
The text was updated successfully, but these errors were encountered: