nestjsx / nestjs-config

Config module for nestjs using dotenv :key:
MIT License
698 stars 44 forks source link

Add type for second return type when using Config.get #138

Open bashleigh opened 5 years ago

bashleigh commented 5 years ago

Issue type:

nestjs-config version

@nestjs/common+core or other package versions

Excepted behavior

I want to be able to use strict typing within my service. Using config.get<string>('something', 'something') will mean type string | undefined is used. Need to be able to specify default type like get<string, string>() where get<T, U>(pattern: string, def?: U): T | U

Actual behavior or outcome (for issue)

Cannot compile because string | undefined doesn't match string

Replication/Example

bashleigh commented 5 years ago

Work around

config.get('swagger.version', 'beta') as string

jeffminsungkim commented 4 years ago

@bashleigh

Not quite sure what you're trying to approach, but here's what I came up with.

class ConfigService {
  get<T, U = T | undefined | null>(param: T | T[], value: U): T | T[] | U {
    // return something
  }
}

const config = new ConfigService();

config.get<string>('hello', 'nestjs');