Skip to content

configService.get<T> returns string #361

@jpramondon

Description

@jpramondon

Issue type:

  • question
  • 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);
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions