You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
`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:
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.
235
235
236
236
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:
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(privateconfigService: 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
0 commit comments