Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cast configuration variable to the provided type #36

Closed
matthiashermsen opened this issue Jan 10, 2020 · 3 comments
Closed

Cast configuration variable to the provided type #36

matthiashermsen opened this issue Jan 10, 2020 · 3 comments

Comments

@matthiashermsen
Copy link

I'm submitting a...


[ ] Regression
[ ] Bug report
[x] Feature request
[ ] Documentation issue or request
[ ] Support request

Current behavior

When fetching configuration values via config service there is no type conversion although I pass in a desired type.

Expected behavior

I would expect the config service to cast the configuration value to the type that I passed in. If that fails, it throws an exception.

Minimal reproduction of the problem with instructions

Create a new application via NestJs CLI.

$ nest new application

Install the config module

$ npm i --save @nestjs/config

Create a .env file in the root directory with the content

PORT = 3000

Update the app.module.ts to

import { Module } from "@nestjs/common";
import { ConfigModule } from "@nestjs/config";

@Module({
  imports: [ConfigModule.forRoot()]
})
export class AppModule {}

Update the main.ts file to

import { NestFactory } from "@nestjs/core";
import { ConfigService } from "@nestjs/config";
import { AppModule } from "./app.module";

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  const config: ConfigService = app.get(ConfigService);
  const port: number = config.get<number>("PORT");

  console.log(port); // 3000
  console.log(typeof port); // string
  console.log(typeof port === "number"); // false

  await app.listen(port);
}
bootstrap();

Start the application via

$ npm run start:dev

You should see the following console logs

  • 3000
  • string
  • false

What is the motivation / use case for changing the behavior?

So the config service usage is described in the docs

https://docs.nestjs.com/techniques/configuration#using-the-configservice

I read something about "type hinting". To me, this is a bit confusing / misleading. If I pass in the type I would expect a numeric cast.

const port: number = config.get<number>("PORT");

You will still receive a string. I found the current code implementation here

https://github.com/nestjs/config/blob/master/lib/config.service.ts

To solve the current example I could do

const port: number = Number(config.get("PORT"));

but a cast would be awesome :)

Environment


Nest common version: 6.7.2
Nest config version: 0.0.7
 
For Tooling issues:
- Node version: 10.16.3
- Platform:  Windows
@prateekkathal
Copy link
Contributor

@matthiashermsen Thanks for the request.

I feel this is something subjective to the use case. Typescript is still evolving and sometimes, some declarations will state you to use a string PORT value than a number.

Also, the generics are meant for the purpose of declaration and not definition. At this stage, I'd say it'd be nice to have an option added to the get() function, say something like below which would cast to a number before returning.

.get("PORT", { castTo: "number" })

However, I'd leave the final decision on @kamilmysliwiec.

Thanks again!

@matthiashermsen
Copy link
Author

Thanks to your reply :) I think you are right. But I was expecting something like this:
When calling

config.get("PORT");

you obviously just get the value as a string. But when providing a "generic" type

config.get<number>("PORT");

then you explicitly want to cast the value before returning it. But yeah, this is just a suggestion and my personal opinion :)

@kamilmysliwiec
Copy link
Member

you obviously just get the value as a string. But when providing a "generic" type

This "generic" type is wiped out after the compilation. The following line of code:

config.get<number>("PORT");

will be eventually transpiled to a vanilla JavaScript:

config.get("PORT");

Hence, we can't inter what type you expect at runtime.

At this stage, I'd say it'd be nice to have an option added to the get() function, say something like below which would cast to a number before returning.

@prateekkathal you should use custom configuration files for this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants