Skip to content

Commit 25bd46e

Browse files
committed
update
1 parent 2bb21e7 commit 25bd46e

File tree

1 file changed

+13
-24
lines changed

1 file changed

+13
-24
lines changed

articles/azure-resource-manager/bicep/variables.md

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,21 @@ The preceding example returns a value like the following:
7979

8080
### Typed variables
8181

82-
Bicep supports **typed variables**, where you explicitly declare the data type of a variable to ensure type safety and improve code clarity. By specifying a type, you help the Bicep compiler catch type-related errors during compilation and make the code more maintainable.
82+
Bicep supports **typed variables**, where you explicitly declare the data type of a variable to ensure type safety and improve code clarity. The Benefits of typed variables:
8383

84-
#### Syntax for typed variables
84+
- **Error detection**: The Bicep compiler validates that assigned values match the declared type, catching errors early.
85+
- **Code clarity**: Explicit types make it clear what kind of data a variable holds.
86+
- **Intellisense support**: Tools like Visual Studio Code provide better autocompletion and validation for typed variables.
87+
- **Refactoring safety**: Ensures that changes to variable assignments don’t inadvertently break type expectations.
88+
89+
To define a typed variable, use the `var` keyword followed by the variable name, a colon (`:`), the type, and the assigned value:
8590

86-
To define a typed variable, use the `var` keyword followed by the variable name, a colon (`:`), the type, and the assigned value.
91+
```bicep
92+
@<decorator>(<argument>)
93+
var <variable-name>: <data-type> = <variable-value>
94+
```
95+
96+
The following examples show how to define typed variables:
8797

8898
```bicep
8999
var resourceName: string = 'myResource'
@@ -93,8 +103,6 @@ var tags: object = { environment: 'dev' }
93103
var subnets: array = ['subnet1', 'subnet2']
94104
```
95105

96-
#### Supported types
97-
98106
Bicep supports the following types for variables:
99107

100108
- **Primitive types**:
@@ -120,8 +128,6 @@ Bicep supports the following types for variables:
120128
var size: 'small' | 'medium' | 'large' = 'medium'
121129
```
122130
123-
#### Object schemas
124-
125131
For `object` types, you can define a schema to enforce a specific structure.
126132
127133
```bicep
@@ -138,15 +144,6 @@ var config: {
138144

139145
The compiler ensures the object adheres to the defined schema.
140146

141-
#### Benefits of typed variables
142-
143-
- **Error detection**: The Bicep compiler validates that assigned values match the declared type, catching errors early.
144-
- **Code clarity**: Explicit types make it clear what kind of data a variable holds.
145-
- **Intellisense support**: Tools like Visual Studio Code provide better autocompletion and validation for typed variables.
146-
- **Refactoring safety**: Ensures that changes to variable assignments don’t inadvertently break type expectations.
147-
148-
#### Example with typed variables
149-
150147
The following example uses typed variables with decorators to enforce constraints:
151148

152149
```bicep
@@ -178,14 +175,6 @@ In this example:
178175
- `resourcePrefix` is typed as `string`.
179176
- `tags` is typed as `object` with a flexible structure.
180177

181-
#### Best practices for typed variables
182-
183-
- **Always specify types**: Explicitly declare types for clarity and to avoid unintended type changes.
184-
- **Use decorators**: Combine typed variables with decorators like `@minValue`, `@maxLength`, or `@allowed` for additional validation.
185-
- **Define object schemas**: For complex objects, use schemas to enforce structure.
186-
- **Keep types simple**: Avoid overly complex union types to maintain readability.
187-
- **Validate with Bicep CLI**: Use `az bicep build` to catch type errors before deployment.
188-
189178
## Use iterative loops
190179

191180
You can use iterative loops when defining a variable. The following example creates an array of objects with three properties.

0 commit comments

Comments
 (0)