Bicep診斷代碼 - BCP364

當 @discriminator() 使用裝飾器,但指定的判別器屬性並非每個工會成員類型必須的字串時,會發生此診斷。 判別子屬性必須是必需的(非可選),且所有聯合成員必須有字串字面型別(而非一般 string 型別)。

Description

「<discriminator-property-name>」必須是所有聯合成員類型中必須的字串字面值。

層級

錯誤

Solutions

確保判別條件:

  • 所有工會會員類型均有且必須(無 ? 後綴)。
  • 以字串字面值(例如 'foo'),型別為一般 string 型別。

Examples

以下範例提出診斷問題,因為該type性質完全缺失於:FooConfig

type FooConfig = {
  value: int
}

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

@discriminator('type')
type ServiceConfig = FooConfig | BarConfig

你可以透過在所有聯合成員類型中加入所需的 type 字串字面屬性來修正診斷問題:

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

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

@discriminator('type')
type ServiceConfig = FooConfig | BarConfig

param config ServiceConfig

以下範例提出診斷問題,因為該type性質在 上FooConfig是可選的(以 ?標記為 ):

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

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

@discriminator('type')
type ServiceConfig = FooConfig | BarConfig

你可以透過將所有工會會員類型要求的 type 屬性來修正診斷:

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

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

@discriminator('type')
type ServiceConfig = FooConfig | BarConfig

param config ServiceConfig

以下範例提出診斷問題,因為 type 上的 FooConfig 屬性是以一般 string 型態而非字串字面型態型別:

type FooConfig = {
  type: string
  value: int
}

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

@discriminator('type')
type ServiceConfig = FooConfig | BarConfig

你可以透過將所有聯集成員類型上的屬性改 type 為字串字面值來修正診斷問題:

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

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

@discriminator('type')
type ServiceConfig = FooConfig | BarConfig

param config ServiceConfig

以下範例提出診斷問題,因為 type 的 FooConfig 性質是整數文字而非字串文字:

type FooConfig = {
  type: 1
  value: int
}

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

@discriminator('type')
type ServiceConfig = FooConfig | BarConfig

你可以透過將所有聯集成員類型上的屬性改 type 為字串字面值來修正診斷問題:

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

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

@discriminator('type')
type ServiceConfig = FooConfig | BarConfig

param config ServiceConfig

以下範例提出診斷問題,因為 type 上的 FooConfig 屬性是字串字面值的聯集,而非單一字串字面值:

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

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

@discriminator('type')
type ServiceConfig = FooConfig | BarConfig

你可以透過在每個聯結成員類型上為屬性指派一個字串字面來 type 修正診斷:

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

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

@discriminator('type')
type ServiceConfig = FooConfig | BarConfig

param config ServiceConfig

下一步

如需 Bicep 診斷的詳細資訊,請參閱 Bicep 核心診斷。