Skip to content

use-union-hierarchy

Id
@azure-tools/typespec-client-generator-core/use-union-hierarchy

Named unions other than string unions should declare an extends constraint, with model variants inheriting from the base and belonging to only one union hierarchy.

Named unions must declare an extends constraint unless all their variants are assignable to string. Numeric, model, and other non-string unions require a constraint, including unions that mix strings with other types. For an object-model constraint such as union Pet extends PetBase, each model variant must be PetBase itself or inherit from that exact model, directly or transitively. A model can be a variant of only one union with an object-model extends constraint. Non-model and array constraints are not subject to the model inheritance or ownership checks. The compiler checks structural assignability, but the union’s extends clause does not create a model inheritance relationship.

Use model Cat extends PetBase to establish the hierarchy required by SDKs in nominally typed languages. Matching properties, spreading PetBase, or writing model Dog is PetBase does not establish that relationship.

model PetBase {
name: string;
}
model Cat {
name: string;
toy: string;
}
model Dog is PetBase;
union Pet extends PetBase {
cat: Cat,
dog: Dog,
}
model PetBase {
name: string;
}
model Cat extends PetBase {
toy: string;
}
model Dog extends PetBase {}
model WorkingDog extends Dog {
job: string;
}
union Pet extends PetBase {
cat: Cat,
dog: WorkingDog,
other: PetBase,
}

Numeric named unions must declare a constraint even when their variants are literals rather than models:

union Priority extends int32 {
low: 1,
high: 2,
}

Array unions still require an extends constraint, but their variants do not need nominal model inheritance or single-union ownership. The compiler checks array element assignability:

model Named {
name: string;
}
model Cat extends Named {
meow: boolean;
}
model Dog extends Named {
bark: boolean;
}
union PetArrays extends Named[] {
cats: Cat[],
dogs: Dog[],
}

Array alternatives may be reused across array-constrained unions. This exemption does not apply to dictionaries or indexed object models, and does not imply SDK emitter support or make Cat[] | Dog[] equivalent to (Cat | Dog)[].

String unions do not have to use extends. This includes string literals, extensible strings, scalars derived from string, and nested string unions:

union Color {
string,
red: "red",
blue: "blue",
}

Mixing strings with numeric, model, or null variants does not qualify for this exemption.

Anonymous union expressions such as Cat | null also do not require extends.

Do not list the same model in multiple unions with object-model extends constraints:

model PetBase {
name: string;
}
model Cat extends PetBase {}
model Dog extends PetBase {}
union Pet extends PetBase {
Cat,
Dog,
}
// Incorrect: Cat already belongs to Pet.
union IndoorPet extends PetBase {
Cat,
}

Reuse Pet rather than defining a second union containing Cat. Distinct unions may share PetBase as their constraint when their model variants are distinct.

  • Area: SDK

Structurally compatible variants without a shared nominal hierarchy can prevent SDKs from representing the union through its declared base type. Explicit model inheritance preserves that relationship for client generation without changing the compiler’s structural assignability rules. Keeping each model variant in a single union hierarchy avoids incompatible SDK inheritance requirements.

Do not suppress this rule. Declare an extends constraint for non-string named unions. When using an object-model constraint, use model extends to establish the required model hierarchy and keep each model variant in one such union. Suppression does not make an unsupported hierarchy representable in nominally typed SDKs.