Indexers in user-defined types
- Dominant language
- Bicep
- Stars
- 3.6k
- Forks
- 830
- Avg merge
- 1d 21m
- Merged PRs (30d)
- 79
Description
At some places, we have objects mapping a fixed set of keys to some values, eg:
```bicep
var regionToUrl = {
northeurope: 'http://example.eu'
uksouth: 'http://example.co.uk'
westus: 'http://example.com'
// rest of known azure regions
}
type regionToUrlType = {
northeurope: string
uksouth: string
westus: string
// rest of known azure regions
}
output regionToUrl regionToUrlType = regionToUrl
```
The problem is that the type of these objects must include a property for each of the allowed keys.
It would be much more convenient to be able to do this:
```bicep
type regionCodeType: 'northeurope' | 'uksouth' | 'westus' // rest of known azure regions
type regionToUrlType = {
[regionCodeType]: string
}
```
This would allow all sorts of static checking, for example the below line should yield an error:
```bicep
// Suppose regionToUrl is typed as regionToUrlType
var regionToUrl = someModule.outputs.regionToUrl
var invalidUrl = regionToUrl['foo'] // Error because 'foo' is not in the allowed keys of regionToUrlType
```
Contributor guide
Research direction
The issue describes index signatures in user-defined types, using a union of region codes and rejecting an invalid indexed key. Start by locating the compiler's type-checking and indexer implementation, then verify that valid keys are accepted and unknown keys produce a diagnostic.
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
- Needs clarification
- Newbie friendliness
- 30/100