Edit

Share via


Bicep diagnostic code - BCP076

This diagnostic occurs when attempting to use the index operator ([] or [^index]) on an expression that isn't an array or object. This happens when the expression being indexed is of an incompatible type, such as a string, integer, boolean, or null.

Description

Can't index over expression of type <wrong-type>. Arrays or objects are required.

Level

Error

Examples

The following code attempts to index a string, which triggers BCP076:

var notAnArray = 'hello'
var invalidAccess = notAnArray[0] // Error: BCP076 - Can't index over expression of type 'hello'. Arrays or objects are required.

This code tries to use the reverse index operator on an integer:

var number = 42
var invalidIndex = number[^1] // Error: BCP076 - Can't index over expression of type '42'. Arrays or objects are required.

To fix the error, ensure the expression is an array or object:

var sizes = ['small', 'medium', 'large']
var first = sizes[0] // Returns 'small'
var last = sizes[^1] // Returns 'large'

var settings = { key1: 'value1', key2: 'value2' }
var value = settings['key1'] // Returns 'value1'

If you need to extract parts of a string, use string functions like split or substring:

var text = 'hello'
var chars = split(text, '') // Returns ['h', 'e', 'l', 'l', 'o']
var firstChar = chars[0] // Returns 'h'

Next steps

For more information about Bicep diagnostics, see Bicep core diagnostics.