Skip to content

Commit 86d5659

Browse files
Merge pull request #252593 from mumian/0921-tagged-union-type
tagged union type declaration
2 parents 45b86d6 + 6646e69 commit 86d5659

File tree

1 file changed

+34
-3
lines changed

1 file changed

+34
-3
lines changed

articles/azure-resource-manager/bicep/user-defined-data-types.md

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: User-defined types in Bicep
33
description: Describes how to define and use user-defined data types in Bicep.
44
ms.topic: conceptual
55
ms.custom: devx-track-bicep
6-
ms.date: 09/20/2023
6+
ms.date: 09/26/2023
77
---
88

99
# User-defined data types in Bicep
@@ -169,8 +169,6 @@ In addition to be used in the `type` statement, type expressions can also be use
169169
param mixedTypeArray ('fizz' | 42 | {an: 'object'} | null)[]
170170
```
171171
172-
## An example
173-
174172
A typical Bicep file to create a storage account looks like:
175173
176174
```bicep
@@ -217,6 +215,39 @@ resource storageAccount 'Microsoft.Storage/storageAccounts@2022-09-01' = {
217215
}
218216
```
219217

218+
## Declare tagged union type
219+
220+
To declare a custom tagged union data type within a Bicep file, you can place a discriminator decorator above a user-defined type declartion. [Bicep version 0.21.1 or newer](./install.md) is required to use this decorator. The syntax is:
221+
222+
```bicep
223+
@discriminator('<propertyName>')
224+
```
225+
226+
The discriminator decorator takes a single parameter, which represents a shared property name among all union members. This property name must be a required string literal on all members and is case-sensitive. The values of the discriminated property on the union members must be unique in a case-insensitive manner.
227+
228+
The following example shows how to declare a tagged union type:
229+
230+
```bicep
231+
type FooConfig = {
232+
type: 'foo'
233+
value: int
234+
}
235+
236+
type BarConfig = {
237+
type: 'bar'
238+
value: bool
239+
}
240+
241+
@discriminator('type')
242+
type ServiceConfig = FooConfig | BarConfig | { type: 'baz', *: string }
243+
244+
param serviceConfig ServiceConfig = { type: 'bar', value: true }
245+
246+
output config object = serviceConfig
247+
```
248+
249+
The parameter value is validated based on the discriminated property value. In the preceeding example, if the *serviceConfig* parameter value is of type *foo*, it undersoes validation using the *FooConfig*type. Likewise, if the parameter value is of type *bar*, validation is performed usin the *BarConfig* type, and this pattern continues for other types as well.
250+
220251
## Import types between Bicep files (Preview)
221252

222253
[Bicep version 0.21.1 or newer](./install.md) is required to use this compile-time import feature. The experimental flag `compileTimeImports` must be enabled from the [Bicep config file](./bicep-config.md#enable-experimental-features).

0 commit comments

Comments
 (0)