Skip to main content
Version: Latest (Core: 0.57.x, Azure: 0.43.x)

no-closed-literal-union

Full name
@azure-tools/typespec-azure-core/no-closed-literal-union

Azure services favor extensible enums to avoid breaking changes as new enum values are added. When using a union of only string or numeric literals it is the equivalent to a closed enum. Adding the base scalar(string, int32, int64, etc.) as a variant to the union makes it extensible.

❌ Incorrect

union PetKind {
Cat: "cat",
Dog: "dog",
}
model Pet {
kind: "cat" | "dog";
}

✅ Correct

union PetKind {
Cat: "Cat",
Dog: "Dog",
string,
}
model Pet {
kind: "cat" | "dog" | string;
}