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

Add option to create user defined types from static data #14715

Open
tehho opened this issue Aug 1, 2024 · 1 comment
Open

Add option to create user defined types from static data #14715

tehho opened this issue Aug 1, 2024 · 1 comment
Labels
enhancement New feature or request type system

Comments

@tehho
Copy link

tehho commented Aug 1, 2024

Is your feature request related to a problem? Please describe.
Would be nice to have the option to add types from static data in bicep.
Main benefit is that you get intelisence on calling suggestedRoleId() that is dynamically updated.

// does not scale
@export()
type RoleName =
  | 'Contributor'
  | 'Owner'
  | 'Reader'
  | 'Private DNS Zone Contributor'
  | 'Network Contributor'
  | 'Log Analytics Reader'
  | 'Log Analytics Contributor'

@export()
func lookupRoleId(name RoleName) string => loadJsonContent('roles.json')[?name] ?? null

Describe the solution you'd like

// do scale
var roles = loadJsonContent('roles.json')

@export()
type SuggestedRoleName = items(roles)

@export()
func suggestedRoleId(name SuggestedRoleName) string => roles[name]
@tehho tehho added the enhancement New feature or request label Aug 1, 2024
@jeskew
Copy link
Contributor

jeskew commented Aug 1, 2024

I like this suggestion! The implementation would likely be a utility type (the <...> syntax introduced for resource-derived types), and I think we'd need a different name to disambiguate from the items function. Perhaps we could borrow TypeScript's terminology and call this keyof?:

type obj = {
  foo: string
  bar: string
}

@sealed()
type sealedObj = {
  foo: string
  bar: string
}

type objKey = keyof<obj>              // == 'foo' | 'bar' | string
type sealedObjKey = keyof<sealedObj>  // == 'foo' | 'bar'

I think we might need a typeof utility type to make this work with the example supplied, though, since roles is a value rather than a type:

// do scale
var roles = loadJsonContent('roles.json')

@export()
type SuggestedRoleName = keyof<typeof<roles>>

@export()
func suggestedRoleId(name SuggestedRoleName) string => roles[name]

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

3 participants