nestjs-config icon indicating copy to clipboard operation
nestjs-config copied to clipboard

configService.get<T> returns string

Open jpramondon opened this issue 3 years ago • 1 comments

Issue type:

  • [ ] question
  • [x] bug report
  • [ ] feature request
  • [ ] documentation issue

nestjs-config version 0.6.3

@nestjs/common+core or other package versions

  • @nestjs/common: 7.6.11
  • @nestjs/core: 7.6.11

Excepted behavior

Given the following .env.local file,

KEY1=3000
KEY2=false

When performing this:

const confValue = this.configService.get<number>("KEY1", 5000);

then confValue's type should be a number. At least that's what the get method's signature suggests and that's great.

Actual behavior or outcome (for issue)

However, when doing:

const confValue = this.configService.get<number>("KEY1", 5000);

... then confValue is actually a string, which is confirmed by typeof confValue

... which gets worse when dealing with booleans:

const confValue = this.configService.get<boolean>("KEY2", false);
if (confValue) {
  // truthy expression which always returns true:  if "false" => true
}

Thanks for your help !

Replication/Example

@Module({
  imports: [
    ConfigModule.forRoot({ envFilePath: ['.env.local'], cache: true })
  ],
  controllers: [ ],
  providers: [MyManager],
})
export class AppModule { }
@Injectable()
export class MyManager {

    @Inject()
    private configService: ConfigService;

    public async doStuff(): Promise<void> {
        const confValue1 = this.configService.get<number>("KEY1", 5000);
        const confValue2 = this.configService.get<boolean>("KEY2", false);
    }
}

jpramondon avatar Feb 08 '21 13:02 jpramondon

nestjs-config version 0.6.3

but nestjs-config never had this version. Maybe you're talking about @nestjs/config?

$ npm info nestjs-config versions
[
  '1.0.0',        '1.0.1',        '1.1.0',
  '1.2.0',        '1.2.1',        '1.2.2',
  '1.2.3',        '1.2.4',        '1.2.5',
  '1.2.6',        '1.2.7',        '1.2.8',
  '1.2.9-alpha',  '1.2.9',        '1.3.0',
  '1.3.2',        '1.3.21',       '1.4.0',
  '1.4.1',        '1.4.2',        '1.4.3',
  '1.4.4',        '1.4.5',        '1.4.6',
  '1.4.7',        '1.4.8',        '2.0.0-beta',
  '2.0.0-beta.1', '2.0.0-beta.2', '2.0.0-beta.3'
]

micalevisk avatar Jul 21 '21 22:07 micalevisk