Skip to content

Commit 83382f8

Browse files
Merge pull request #2085 from micalevisk/new-configuration-feat
docs(): add new feature of ConfigService on configuration page
2 parents b7d8a55 + 6bd670a commit 83382f8

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

content/techniques/configuration.md

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ The `get()` method also takes an optional second argument defining a default val
214214
const dbHost = this.configService.get<string>('database.host', 'localhost');
215215
```
216216

217-
`ConfigService` has an optional generic (type argument) to help prevent accessing a config property that does not exist. Use it as shown below:
217+
`ConfigService` has two optional generics (type arguments). The first one is to help prevent accessing a config property that does not exist. Use it as shown below:
218218

219219
```typescript
220220
interface EnvironmentVariables {
@@ -226,19 +226,31 @@ interface EnvironmentVariables {
226226
constructor(private configService: ConfigService<EnvironmentVariables>) {
227227
const port = this.configService.get('PORT', { infer: true });
228228

229-
// Error: this is invalid as the URL property is not defined
229+
// TypeScript Error: this is invalid as the URL property is not defined in EnvironmentVariables
230230
const url = this.configService.get('URL', { infer: true });
231231
}
232232
```
233233

234-
With the `infer` property set to `true`, the `ConfigService#get` method will automatically infer the property type based on the interface, so for example, `typeof port === "number"` since `PORT` has a `number` type in the `EnvironmentVariables` interface.
234+
With the `infer` property set to `true`, the `ConfigService#get` method will automatically infer the property type based on the interface, so for example, `typeof port === "number"` (if you're not using `strictNullChecks` flag from TypeScript) since `PORT` has a `number` type in the `EnvironmentVariables` interface.
235235

236236
Also, with the `infer` feature, you can infer the type of a nested custom configuration object's property, even when using dot notation, as follows:
237237

238238
```typescript
239239
constructor(private configService: ConfigService<{ database: { host: string } }>) {
240-
const dbHost = this.configService.get('database.host', { infer: true });
241-
// typeof dbHost === "string"
240+
const dbHost = this.configService.get('database.host', { infer: true })!;
241+
// typeof dbHost === "string" |
242+
// +--> non-null assertion operator
243+
}
244+
```
245+
246+
The second generic relies on the first one, acting as a type assertion to get rid of all `undefined` types that `ConfigService`'s methods can return when `strictNullChecks` is on. For instance:
247+
248+
```typescript
249+
// ...
250+
constructor(private configService: ConfigService<{ PORT: number }, true>) {
251+
// ^^^^
252+
const port = this.configService.get('PORT', { infer: true });
253+
// ^^^ The type of port will be 'number' thus you don't need TS type assertions anymore
242254
}
243255
```
244256

0 commit comments

Comments
 (0)