Azure / Azure/bicep

Tagged union types: type-guarded completions for properties

Open
#11,293 0 comments 1 reaction 0 assignees View on GitHub
enhancement type system
Dominant language
Bicep
Stars
3.6k
Forks
830
Avg merge
1d 21m
Merged PRs (30d)
79

Description

**Is your feature request related to a problem? Please describe.**
Extension of https://github.com/Azure/bicep/issues/9230 to support completions of tagged union types. When doing property access on a tagged union type, only the discriminator property is provided in completions because it exists in all union members. The completions need to be extended to support completions inside of scopes where an object of the union type has been discriminated/type-guarded.

**Describe the solution you'd like**

Take the following example where `|` represents where completions relevant to this issue would be provided:

```bicep
type typeA = {
type: 'a'
value: string
}

type typeB = {
type: 'b'
value: int
}

@discriminator('type')
type unionType = typeA | typeB

param unionParam unionType

resource r1 '...' = {
name: '...'
properties: {
prop: unionParam.type == 'a' ? unionParam.| : unionParam.| // first position: should be completions for type A. Second position: should be only a type completion or anything except 'a'.
}
}

resource r2 '...' = if (unionParam.type == 'a') {
name: '...'
properties: {
prop: unionParam.|
}
}
```

In these cases, since `unionParam` is discriminated by the condition checks, a certain set of completions sourced from the union members could be provided.

If the type-guard is simple, such as checking if the discriminator property equals exactly one value, property completions for that specific union member could be provided.

If conditions are more complex, such as checking if the discriminator property is equal to 2 or more values, then property completions would need to either be limited to just the discriminator or limited to the overlap between all possible members where property types are the same. Including all completions across possible members will likely lead to confusion/error and conflict when completion items are generated, such as members having overlapping property names with different types. In general, completions should likely be limited to safe options.

If conditions include checks for discriminator property values but include other conditions that cannot be evaluated until deploy time, completions will need to be limited to safe property completions.

It may be useful to take a look at the existing TypeScript implementation for how completions are suggested in various type guarded scenarios.

Contributor guide

Open the contributing guide

Research direction

Start by reading issue 9230 and the existing TypeScript implementation for type-guarded completions. Compare the simple and complex discriminator checks described here, then determine how safe completions should behave for each case. Done means tagged-union property access offers valid, non-conflicting completions without suggesting unsafe members.

Written by the indexing model from the issue text.

Assessment

Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.