bicep icon indicating copy to clipboard operation
bicep copied to clipboard

Narrowing assignment to union typed receiver should generate warning, not error

Open jeskew opened this issue 2 years ago • 0 comments

Is your feature request related to a problem? Please describe. If a value whose type is an unmodified string is assigned to a receiver expecting one of a fixed set of strings, Bicep will emit an error-level diagnostic. For example:

main.bicep

param typedObjectParam {
  stringProperty: string
}

module mod 'mod.bicep' = {
  name: 'mod'
  params: {
    foobar: typedObjectParam.stringProperty
  }
}

mod.bicep

@allowed(['foo', 'bar'])
param foobar string

Diagnostics:

PS Z:\dir> az bicep build -f .\main.bicep     
Z:\dir\main.bicep(8,13) : Error BCP036: The property "foobar" expected a value of type "'bar' | 'foo'" but the provided value is of type "string".

Describe the solution you'd like The value and receiver do not have disjoint types -- a value of 'bar' | 'foo' would be assignable to a receiver of type string -- so the BCP036 diagnostic should be a warning since it may succeed at runtime. Bicep should also treat this as a narrowing assignment, since if the assignment succeeds, then the runtime value will meet the receiver's constraints. This would be in line with how we handle the assignment of values that may be too small or large for their receiver.

jeskew avatar Sep 19 '23 18:09 jeskew