Azure / Azure/bicep

Inheritance for User-Defined Types

Open
#14,135 11 comments 17 reactions 1 assignee Claimed by @jeskew View on GitHub
1.0 - Consider discussion enhancement size: extra large type system
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.**

This feature request is not related to a problem I have, but more like an improvement on User-Defined Types (UDT). I am looking at UDTs like a class in TypeScript or C# for example. I know it's a different concept in Bicep, but it looks similar.

What would be great is to have the same concept of class inheritance in User-Defined Types. So, to have some kind of `base` type where other UDTs can `extend` or `inherit` from that base type.

**Describe the solution you'd like**
A clear and concise description of what you want to happen.

I will provide a practical example. In my recent PR in the Azure Verified Modules (https://github.com/Azure/bicep-registry-modules/pull/1931) repository I tried to achieve some kind of "inheritance" this way:

```bicep
type zoneBaseType = {
name: string
metadata: object?
ttl: int?
roleAssignments: roleAssignmentType
}
```

In the `zoneBaseType` UDT the base properties are defined that are used in every record type (a, aaaa, cname etc.) as seen below:

```bicep
type aType = {
@description('Required. The base properties of the record.')
base: zoneBaseType

targetResourceId: string?

aRecords: {
ipv4Address: string
}[]?
}[]?
```

The outcome is what I wanted, but not quite what I had in mind.

What would be great functionality to have is inheritance like we see in C# and TypeScript, so we can remove the need for a property like the `base: zoneBaseType` property.

How I see it with inheritance:

```bicep
type baseType = {
name: string
metadata: object?
ttl: int?
roleAssignments: roleAssignmentType
}

type aType extends baseType = {
targetResourceId: string?
aRecords: {
ipv4Address: string
}[]
}[]?
```

The type `aType` now extends from `baseType`, so when using the UDT `aType` you make use of the following properties:
* [inherited / extended from baseType] name: string
* [inherited / extended from baseType] metadata : object?
* [inherited / extended from baseType] ttl: int?
* [inherited / extended from baseType] roleAssignment: roleAssignmentType
* targetResourceId: string?
* aRecords: object

I hope this is clear and understandable. If not, please let me know.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.