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

Add type for second return type when using Config.get

Open bashleigh opened this issue 4 years ago • 2 comments

Issue type:

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

nestjs-config version

@nestjs/common+core or other package versions

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

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 avatar Aug 14 '19 13:08 bashleigh

Work around

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

bashleigh avatar Aug 14 '19 13:08 bashleigh

@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');

jeffminsungkim avatar Dec 21 '19 02:12 jeffminsungkim