Skip to content

Commit e1cb64b

Browse files
NarHakobyanBurak Tasci
authored and
Burak Tasci
committed
feat(getSettings): added generic type for getSetting function (#146)
* feat(getSettings): added generic type for getSetting function * feat(docs): updated docs, added generic types * style(core): remove type casting * docs(readme): update readme
1 parent 3fe1c70 commit e1cb64b

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed

packages/@ngx-config/core/README.md

+10-8
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,8 @@ You can find detailed information about the usage guidelines for the `ConfigMerg
141141
When the `getSettings` method is invoked without parameters, it returns entire application configuration. However, the `getSettings`
142142
method can be invoked using two optional parameters: **`key`** and **`defaultValue`**.
143143

144+
To specify returning value type you can add generic type in `getSettings`.
145+
144146
The following example shows how to read configuration settings using all available overloads of `getSettings` method.
145147

146148
#### anyclass.ts
@@ -156,42 +158,42 @@ export class AnyClass {
156158

157159
myMethodToGetUrl1a() {
158160
// will retrieve 'http://localhost:8000'
159-
let url:string = this.config.getSettings('system.applicationUrl');
161+
const url = this.config.getSettings<string>('system.applicationUrl');
160162
}
161163

162164
myMethodToGetUrl1b() {
163165
// will retrieve 'http://localhost:8000'
164-
let url:string = this.config.getSettings(['system', 'applicationUrl']);
166+
const url = this.config.getSettings<string>(['system', 'applicationUrl']);
165167
}
166168

167169
myMethodToGetUrl2a() {
168170
// will retrieve 'http://localhost:8000'
169-
let url:string = this.config.getSettings('system').applicationUrl;
171+
const url = this.config.getSettings<string>('system').applicationUrl;
170172
}
171173

172174
myMethodToGetUrl2b() {
173175
// will retrieve 'http://localhost:8000'
174-
let url:string = this.config.getSettings().system.applicationUrl;
176+
const url = this.config.getSettings<string>().system.applicationUrl;
175177
}
176178

177179
myMethodToGetUrl3a() {
178180
// will throw an exception (system.non_existing is not in the application settings)
179-
let url:string = this.config.getSettings('system.non_existing');
181+
const url = this.config.getSettings<string>('system.non_existing');
180182
}
181183

182184
myMethodToGetUrl3b() {
183185
// will retrieve 'no data' (system.non_existing is not in the application settings)
184-
let url:string = this.config.getSettings('system.non_existing', 'no data');
186+
const url = this.config.getSettings<string>('system.non_existing', 'no data');
185187
}
186188

187189
myMethodToGetSeo1() {
188190
// will retrieve {"pageTitle":"Tweeting bird"}
189-
let seoSettings: string = this.config.getSettings('seo');
191+
const seoSettings = this.config.getSettings<string>('seo');
190192
}
191193

192194
myMethodToGetSeo1() {
193195
// will retrieve {"pageTitle":"Tweeting bird"}
194-
let seoSettings: string = this.config.getSettings().seo;
196+
const seoSettings = this.config.getSettings<string>().seo;
195197
}
196198
}
197199
```

packages/@ngx-config/core/src/config.service.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export class ConfigService {
1616
.then((res: any) => this.settings = res);
1717
}
1818

19-
getSettings(key?: string | Array<string>, defaultValue?: any): any {
19+
getSettings<T = any>(key?: string | Array<string>, defaultValue?: any): T {
2020
if (!key || (Array.isArray(key) && !key[0]))
2121
return this.settings;
2222

0 commit comments

Comments
 (0)