分享方式:


在 Bicep 中使用者自訂資料類型

了解如何在 Bicep 中定義和使用使用者定義的資料類型。 如需系統定義的數據類型,請參閱 數據類型

需要 Bicep CLI 0.12.X 版或更高版本才能使用此功能。

使用者定義資料類型語法

您可以使用 type 陳述式來定義使用者定義的資料類型。 此外,您也可以在某些地方使用類型運算式來定義自訂類型。

type <user-defined-data-type-name> = <type-expression>

注意

@allowed裝飾項目僅能在param陳述式上。 若要宣告該屬性必須是 typeoutput 陳述式中一組預先定義的值之一,請使用等位型別語法。 等位型別語法也可以在param陳述式使用。

有效的型別運算式包括:

  • 符號參考是參考環境型別的識別碼 (例如 stringint),或是 type 陳述式中宣告的使用者定義型別符號:

    // Bicep data type reference
    type myStringType = string
    
    // user-defined type reference
    type myOtherStringType = myStringType
    
  • 基本常值,包括字串、整數和布林值,都是有效的型別運算式。 例如:

    // a string type with three allowed values.
    type myStringLiteralType = 'bicep' | 'arm' | 'azure'
    
    // an integer type with one allowed value
    type myIntLiteralType = 10
    
    // an boolean type with one allowed value
    type myBoolLiteralType = true
    
  • 陣列類型可以藉由將 [] 後綴宣告為任何有效的型別運算式:

    // A string type array
    type myStrStringsType1 = string[]
    // A string type array with three allowed values
    type myStrStringsType2 = ('a' | 'b' | 'c')[]
    
    type myIntArrayOfArraysType = int[][]
    
    // A mixed-type array with four allowed values
    type myMixedTypeArrayType = ('fizz' | 42 | {an: 'object'} | null)[]
    
  • 物件類型包含大括弧之間的零個或多個屬性:

    type storageAccountConfigType = {
      name: string
      sku: string
    }
    

    物件中的每個屬性都是由索引鍵和值所組成。 索引鍵和值以冒號 : 分隔。 索引鍵可以是任何字串 (不是有效標識符的值必須以引號括住),值可以是任何類型語法運算式。

    除非屬性在屬性值之後有選擇性標記 ?,否則需要屬性。 例如,下列範例中的 sku 屬性是選擇性的:

    type storageAccountConfigType = {
      name: string
      sku: string?
    }
    

    裝飾項目可用於屬性。 * 可用來讓所有值都需要限制式。 使用 * 時,仍可能會定義其他屬性。 這個範例會建立物件,該物件需要名為 id 的索引鍵,而且物件中的所有其他專案都必須是長度至少 10 個字元的字串值。

    type obj = {
      @description('The object ID')
      id: int
    
      @description('Additional properties')
      @minLength(10)
      *: string
    }
    

    下列範例示範如何使用等位型別語法來列出一組預先定義的值:

    type obj = {
      level: 'bronze' | 'silver' | 'gold'
    }
    

    遞迴

    物件類型可能會使用直接或間接遞迴,所以遞迴點的路徑至少有一段是選擇性的。 例如,下列範例中的 myObjectType 定義有效,因為直接遞迴 recursiveProp 屬性是選擇性的:

    type myObjectType = {
      stringProp: string
      recursiveProp: myObjectType?
    }
    

    但是下列類型定義無效,因為 level1level2level3level4level5 都是選擇性的。

    type invalidRecursiveObjectType = {
      level1: {
        level2: {
          level3: {
            level4: {
              level5: invalidRecursiveObjectType
            }
          }
        }
      }
    }
    
  • Bicep 一元運算子可以搭配整數和布林常值或整數或布林常值型別符號的參考使用:

    type negativeIntLiteral = -10
    type negatedIntReference = -negativeIntLiteral
    
    type negatedBoolLiteral = !true
    type negatedBoolReference = !negatedBoolLiteral
    
  • 等位可能包含任意數目的常值型別運算式。 等位型別會轉譯成 Bicep 中允許值限制式,因此只有常值可以做為成員。

    type oneOfSeveralObjects = {foo: 'bar'} | {fizz: 'buzz'} | {snap: 'crackle'}
    type mixedTypeArray = ('fizz' | 42 | {an: 'object'} | null)[]
    

除了在 type 陳述式中使用,型別表示式也可用於這些位置來建立使用者定義的資料類型:

  • param 陳述式的型別子句中。 例如:

    param storageAccountConfig {
      name: string
      sku: string
    }
    
  • 在物件類型屬性中 : 之後。 例如:

    param storageAccountConfig {
     name: string
      properties: {
        sku: string
      }
    } = {
      name: 'store$(uniqueString(resourceGroup().id)))'
      properties: {
        sku: 'Standard_LRS'
      }
    }
    
  • 在陣語型別運算式的 [] 前面。 例如:

    param mixedTypeArray ('fizz' | 42 | {an: 'object'} | null)[]
    

建立儲存體帳戶的一般 Bicep 檔案看起來如下:

param location string = resourceGroup().location
param storageAccountName string

@allowed([
  'Standard_LRS'
  'Standard_GRS'
])
param storageAccountSKU string = 'Standard_LRS'

resource storageAccount 'Microsoft.Storage/storageAccounts@2022-09-01' = {
  name: storageAccountName
  location: location
  sku: {
    name: storageAccountSKU
  }
  kind: 'StorageV2'
}

藉由使用使用者定義的資料類型,其看起來如下:

param location string = resourceGroup().location

type storageAccountSkuType = 'Standard_LRS' | 'Standard_GRS'

type storageAccountConfigType = {
  name: string
  sku: storageAccountSkuType
}

param storageAccountConfig storageAccountConfigType

resource storageAccount 'Microsoft.Storage/storageAccounts@2022-09-01' = {
  name: storageAccountConfig.name
  location: location
  sku: {
    name: storageAccountConfig.sku
  }
  kind: 'StorageV2'
}

宣告標記的等位型別

若要在 Bicep 檔案中宣告自定義標記聯集數據類型,您可以將歧視性裝飾專案放在使用者定義的類型宣告上方。 需要 Bicep CLI 0.21.X 版或更高版本才能使用此裝飾項目。 語法為:

@discriminator('<propertyName>')

鑑別子屬性裝飾項目會採用單一參數,代表所有集合聯集成員之間的共用屬性名稱。 這個屬性名稱必須是所有成員的必要字串常值,而且區分大小寫。 等位成員上鑑別子屬性的值必須是唯一的,且不區分大小寫。

下列範例示範如何宣告標記的等位型別:

type FooConfig = {
  type: 'foo'
  value: int
}

type BarConfig = {
  type: 'bar'
  value: bool
}

@discriminator('type')
type ServiceConfig = FooConfig | BarConfig | { type: 'baz', *: string }

param serviceConfig ServiceConfig = { type: 'bar', value: true }

output config object = serviceConfig

參數值會根據鑑別子屬性的屬性值進行驗證。 在上述範例中,如果 serviceConfig 參數值的類型為 foo,則會使用 FooConfig 類型進行驗證。 同樣地,如果參數值的類型為,則會使用 BarConfig 類型執行驗證,而且其他類型也會繼續此模式。

在 Bicep 檔案之間匯入類別 (預覽)

需要 Bicep CLI 0.21.X 版或更高版本才能使用此編譯時間匯入功能。 實驗性旗標 compileTimeImports 必須從 Bicep 設定檔啟用。

只有具有 @export() 裝飾項目的使用者定義資料類型可以匯入至其他範本。 目前,此裝飾項目只能在 type 陳述式上使用。

下列範例可讓您從其他樣本匯入兩個使用者定義的資料類型:

@export()
type myStringType = string

@export()
type myOtherStringType = myStringType

如需詳細資訊,請參閱匯入使用者定義的資料類型

下一步

  • 若要 Bicep 資料類型的清單,請參閱資料類型