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
Is there an existing issue that is already proposing this?
I have searched the existing issues
Is your feature request related to a problem? Please describe it
Using Configuration namespaces is pretty powerful as it enhances type safety and reduces amount of boilerplate code. However, configuration object is a mutable singleton shared across the whole application. There are no limitations for mutating configuration object in one service, which may affect other services.
Describe the solution you'd like
Adding readonly config type directly to framework could prevent config mutation errors on compile time and reduce amount of boilerplate code which is similar across projects.
API
There are 3 possible APIs for this:
Making ConfigType infer readonly config type by default. I think it is the best solution, however it introduces a BREAKING CHANGE
This solution doesn't affect existing codebases, however it adds possibility to make ConfigType to infer readonly config type. The API is similar to WasValidated param in ConfigService. I don't like this solution as adding extra param is a bit confusing in my opinion.
Introducing a new ReadonlyConfigType type. In my opinion new type is more expressive than the second solution.
Implementation
There are 2 ways to implement readonly config type:
Naive ReadonlyDeep. This implementation is limited as it doesn't narrow arrays for tuples for example and doesn't work with arrays of objects.
...
@Injectable()exportclassAppService{constructor(
@Inject(config.KEY)privatereadonlyconfiguration: ConfigType<typeofconfig>){}mutateConfig(){// port now is globally equal to 3001// No ts errorsthis.configuration.port=3001;}}
Adding as const to config registration doesn't solve the issue as ConfigType still infers as a mutable config type:
Is there an existing issue that is already proposing this?
Is your feature request related to a problem? Please describe it
Using Configuration namespaces is pretty powerful as it enhances type safety and reduces amount of boilerplate code. However, configuration object is a mutable singleton shared across the whole application. There are no limitations for mutating configuration object in one service, which may affect other services.
Describe the solution you'd like
Adding readonly config type directly to framework could prevent config mutation errors on compile time and reduce amount of boilerplate code which is similar across projects.
API
There are 3 possible APIs for this:
Making
ConfigType
infer readonly config type by default. I think it is the best solution, however it introduces a BREAKING CHANGEChanging
ConfigType
signature:This solution doesn't affect existing codebases, however it adds possibility to make
ConfigType
to infer readonly config type. The API is similar toWasValidated
param inConfigService
. I don't like this solution as adding extra param is a bit confusing in my opinion.Introducing a new
ReadonlyConfigType
type. In my opinion new type is more expressive than the second solution.Implementation
There are 2 ways to implement readonly config type:
Naive
ReadonlyDeep
. This implementation is limited as it doesn't narrow arrays for tuples for example and doesn't work with arrays of objects.Recursive
ReadonlyDeep
see type-fest ReadonlyDeep implementation.What is the motivation / use case for changing the behavior?
.env
src/config.ts
src/app.service.ts
Adding
as const
to config registration doesn't solve the issue asConfigType
still infers as a mutable config type:For now the only way to address this issue is to manually narrow inferred config type to readonly:
The text was updated successfully, but these errors were encountered: