Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tagged union types: type-guarded completions for properties #11293

Open
kalbert312 opened this issue Jul 17, 2023 · 0 comments
Open

Tagged union types: type-guarded completions for properties #11293

kalbert312 opened this issue Jul 17, 2023 · 0 comments
Labels
enhancement New feature or request type system

Comments

@kalbert312
Copy link
Member

Is your feature request related to a problem? Please describe.
Extension of #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:

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request type system
Projects
Status: Todo
Development

No branches or pull requests

2 participants