Narrowing assignment to union typed receiver should generate warning, not error
- Dominant language
- Bicep
- Stars
- 3.6k
- Forks
- 830
- Avg merge
- 1d 2h
- Merged PRs (30d)
- 79
Description
**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
```bicep
param typedObjectParam {
stringProperty: string
}
module mod 'mod.bicep' = {
name: 'mod'
params: {
foobar: typedObjectParam.stringProperty
}
}
```
mod.bicep
```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.
Contributor guide
Assessment
This issue has not been assessed yet.