Skip to content

Commit 4f99516

Browse files
authored
docs(techniques/configuration): Document how to get the whole nested configuration object
Documentation was missing the fact that you can retrieve the whole nested configuration object, and you can use an interface as a type hint in this case.
1 parent 4c55765 commit 4f99516

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

content/techniques/configuration.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,23 @@ const dbUser = this.configService.get<string>('DATABASE_USER');
146146
const dbHost = this.configService.get<string>('database.host');
147147
```
148148

149-
As shown above, use the `configService.get()` method to get a simple environment variable by passing the variable name. You can do TypeScript type hinting by passing the type, as shown above (e.g., `get<string>(...)`). The `get()` method can also traverse a nested custom configuration object (created via a <a href="techniques/configuration#custom-configuration-files">Custom configuration file</a>), as shown in the second example above. The `get()` method also takes an optional second argument defining a default value, which will be returned when the key doesn't exist, as shown below:
149+
As shown above, use the `configService.get()` method to get a simple environment variable by passing the variable name. You can do TypeScript type hinting by passing the type, as shown above (e.g., `get<string>(...)`). The `get()` method can also traverse a nested custom configuration object (created via a <a href="techniques/configuration#custom-configuration-files">Custom configuration file</a>), as shown in the second example above.
150+
151+
You can also get the whole nested custom configuration object using an interface as the type hint:
152+
153+
```typescript
154+
interface DatabaseConfig {
155+
host: string;
156+
port: number;
157+
}
158+
159+
const dbConfig = this.configService.get<DatabaseConfig>('database');
160+
161+
// you can now use `dbConfig.port` and `dbConfig.host`
162+
const port = dbConfig.port;
163+
```
164+
165+
The `get()` method also takes an optional second argument defining a default value, which will be returned when the key doesn't exist, as shown below:
150166

151167
```typescript
152168
// use "localhost" when "database.host" is not defined

0 commit comments

Comments
 (0)