|
| 1 | +--- |
| 2 | +title: User-defined types in Bicep |
| 3 | +description: Describes how to define and use user-defined data types in Bicep. |
| 4 | +ms.topic: conceptual |
| 5 | +ms.date: 01/09/2023 |
| 6 | +--- |
| 7 | + |
| 8 | +# User-defined data types in Bicep (Preview) |
| 9 | + |
| 10 | +Learn how to use user-defined data types in Bicep. |
| 11 | + |
| 12 | +[Bicep version 1.2 or newer](./install.md) is required to use this feature. |
| 13 | + |
| 14 | +## Enable the preview feature |
| 15 | + |
| 16 | +To enable this preview, modify your project's [bicepconfig.json](./bicep-config.md) file to include the following JSON: |
| 17 | + |
| 18 | +```json |
| 19 | +{ |
| 20 | + "experimentalFeaturesEnabled": { |
| 21 | + "userDefinedTypes": true |
| 22 | + } |
| 23 | +} |
| 24 | +``` |
| 25 | + |
| 26 | +## User-defined data type syntax |
| 27 | + |
| 28 | +You can use the `type` statement to define user-defined data types. In addition, you can also use type expressions in some places to define custom types. |
| 29 | + |
| 30 | +```bicep |
| 31 | +Type <userDefinedDataTypeName> = <typeExpression> |
| 32 | +``` |
| 33 | + |
| 34 | +The valid type expressions include: |
| 35 | + |
| 36 | +- Symbolic references are identifiers that refer to an *ambient* type (like `string` or `int`) or a user-defined type symbol declared in a `type` statement: |
| 37 | + |
| 38 | + ```bicep |
| 39 | + // Bicep data type reference |
| 40 | + type myStringType = string |
| 41 | +
|
| 42 | + // user-defined type reference |
| 43 | + type myOtherStringType = myStringType |
| 44 | + ``` |
| 45 | +
|
| 46 | +- Primitive literals, including strings, integers, and booleans, are valid type expressions. For example: |
| 47 | +
|
| 48 | + ```bicep |
| 49 | + // a string type with three allowed values. |
| 50 | + type myStringLiteralType = 'bicep' | 'arm' | 'azure' |
| 51 | +
|
| 52 | + // an integer type with one allowed value |
| 53 | + type myIntLiteralType = 10 |
| 54 | +
|
| 55 | + // an boolean type with one allowed value |
| 56 | + type myBoolLiteralType = true |
| 57 | + ``` |
| 58 | +
|
| 59 | +- Array types can be declared by suffixing `[]` to any valid type expression: |
| 60 | +
|
| 61 | + ```bicep |
| 62 | + // A string type array |
| 63 | + type myStrStringsType1 = string[] |
| 64 | + // A string type array with three allowed values |
| 65 | + type myStrStringsType2 = ('a' | 'b' | 'c')[] |
| 66 | +
|
| 67 | + type myIntArrayOfArraysType = int[][] |
| 68 | +
|
| 69 | + // A mixed-type array with four allowed values |
| 70 | + type myMixedTypeArrayType = ('fizz' | 42 | {an: 'object'} | null)[] |
| 71 | + ``` |
| 72 | +
|
| 73 | +- Object types contain zero or more properties between curly brackets: |
| 74 | +
|
| 75 | + ```bicep |
| 76 | + type storageAccountConfigType = { |
| 77 | + name: string |
| 78 | + sku: string |
| 79 | + } |
| 80 | + ``` |
| 81 | +
|
| 82 | + Each property in an object consists of key and value. The key and value are separated by a colon `:`. The key may be any string (values that would not be a valid identifier must be enclosed in quotes), and the value may be any type syntax expression. |
| 83 | +
|
| 84 | + Properties are required unless they have an optionality marker `?` between the property name and the colon. For example, the `sku` property in the following example is optional: |
| 85 | +
|
| 86 | + ```bicep |
| 87 | + type storageAccountConfigType = { |
| 88 | + name: string |
| 89 | + sku?: string |
| 90 | + } |
| 91 | + ``` |
| 92 | +
|
| 93 | + **Recursion** |
| 94 | +
|
| 95 | + Object types may use direct or indirect recursion so long as at least leg of the path to the recursion point is optional. For example, the `myObjectType` definition in the following example is valid because the directly recursive `recursiveProp` property is optional: |
| 96 | +
|
| 97 | + ```bicep |
| 98 | + type myObjectType = { |
| 99 | + stringProp: string |
| 100 | + recursiveProp?: myObjectType |
| 101 | + } |
| 102 | + ``` |
| 103 | +
|
| 104 | + But the following would not be valid because none of `level1`, `level2`, `level3`, `level4`, or `level5` is optional. |
| 105 | +
|
| 106 | + ```bicep |
| 107 | + type invalidRecursiveObjectType = { |
| 108 | + level1: { |
| 109 | + level2: { |
| 110 | + level3: { |
| 111 | + level4: { |
| 112 | + level5: invalidRecursiveObject |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + ``` |
| 119 | +
|
| 120 | +- [Bicep unary operators](./operators.md) can be used with integer and boolean literals or references to integer or boolean literal-typed symbols: |
| 121 | +
|
| 122 | + ```bicep |
| 123 | + type negativeIntLiteral = -10 |
| 124 | + type negatedIntReference = -negativeIntLiteral |
| 125 | +
|
| 126 | + type negatedBoolLiteral = !true |
| 127 | + type negatedBoolReference = !negatedBoolLiteral |
| 128 | + ``` |
| 129 | +
|
| 130 | +- Unions may include any number of literal-typed expressions. Union types are translated into the [allowed-value constraint](./parameters.md#decorators) in Bicep, so only literals are permitted as members. |
| 131 | +
|
| 132 | + ```bicep |
| 133 | + type oneOfSeveralObjects = {foo: 'bar'} | {fizz: 'buzz'} | {snap: 'crackle'} |
| 134 | + type mixedTypeArray = ('fizz' | 42 | {an: 'object'} | null)[] |
| 135 | + ``` |
| 136 | +
|
| 137 | +In addition to be used in the `type` statement, type expressions can also be used in these places for creating user-defined date types: |
| 138 | +
|
| 139 | +- As the type clause of a `param` statement. For example: |
| 140 | +
|
| 141 | + ```bicep |
| 142 | + param storageAccountConfig { |
| 143 | + name: string |
| 144 | + sku: string |
| 145 | + } |
| 146 | + ``` |
| 147 | +
|
| 148 | +- Following the `:` in an object type property. For example: |
| 149 | +
|
| 150 | + ```bicep |
| 151 | + param storageAccountConfig { |
| 152 | + name: string |
| 153 | + properties: { |
| 154 | + sku: string |
| 155 | + } |
| 156 | + } = { |
| 157 | + name: 'store$(uniqueString(resourceGroup().id)))' |
| 158 | + properties: { |
| 159 | + sku: 'Standard_LRS' |
| 160 | + } |
| 161 | + } |
| 162 | + ``` |
| 163 | +
|
| 164 | +- Preceding the `[]` in an array type expression. For example: |
| 165 | +
|
| 166 | + ```bicep |
| 167 | + param mixedTypeArray ('fizz' | 42 | {an: 'object'} | null)[] |
| 168 | + ``` |
| 169 | +
|
| 170 | +## An example |
| 171 | +
|
| 172 | +A typical Bicep file to create a storage account looks like: |
| 173 | +
|
| 174 | +```bicep |
| 175 | +param location string = resourceGroup().location |
| 176 | +param storageAccountName string |
| 177 | +
|
| 178 | +@allowed([ |
| 179 | + 'Standard_LRS' |
| 180 | + 'Standard_GRS' |
| 181 | +]) |
| 182 | +param storageAccountSKU string = 'Standard_LRS' |
| 183 | +
|
| 184 | +resource storageAccount 'Microsoft.Storage/storageAccounts@2022-09-01' = { |
| 185 | + name: storageAccountName |
| 186 | + location: location |
| 187 | + sku: { |
| 188 | + name: storageAccountSKU |
| 189 | + } |
| 190 | + kind: 'StorageV2' |
| 191 | +} |
| 192 | +``` |
| 193 | + |
| 194 | +By using user-defined data types, it can look like: |
| 195 | + |
| 196 | +```bicep |
| 197 | +param location string = resourceGroup().location |
| 198 | +
|
| 199 | +type storageAccountSkuType = 'Standard_LRS' | 'Standard_GRS' |
| 200 | +
|
| 201 | +type storageAccountConfigType = { |
| 202 | + name: string |
| 203 | + sku: storageAccountSkuType |
| 204 | +} |
| 205 | +
|
| 206 | +param storageAccountConfig storageAccountConfigType |
| 207 | +
|
| 208 | +resource storageAccount 'Microsoft.Storage/storageAccounts@2022-09-01' = { |
| 209 | + name: storageAccountConfig.name |
| 210 | + location: location |
| 211 | + sku: { |
| 212 | + name: storageAccountConfig.sku |
| 213 | + } |
| 214 | + kind: 'StorageV2' |
| 215 | +} |
| 216 | +``` |
| 217 | + |
| 218 | +## Next steps |
| 219 | + |
| 220 | +- For a list of the Bicep date types, see [Data types](./data-types.md). |
0 commit comments