composition-over-inheritance
@azure-tools/typespec-azure-core/composition-over-inheritanceCheck that if a model is used in an operation and has derived models that it has a discriminator or recommend to use composition via spread or is.
Models that extend a base model without a discriminator should use composition (... spread or model is) instead of inheritance (extends). If polymorphism is intended, add the @discriminator decorator on the base model.
Impact
Section titled “Impact”- Area: SDK
Deep inheritance is hard for SDKs to represent and reduces client usability.
❌ Incorrect
Section titled “❌ Incorrect”Inheritance without a discriminator:
model Pet { name: string;}
model Cat extends Pet { meow: boolean;}Extending a template instance:
model Pet<T> { t: T;}
model Cat extends Pet<string> {}✅ Correct
Section titled “✅ Correct”Using composition with spread:
model Pet { name: string;}
model Cat { ...Pet; meow: boolean;}Using model is:
model Pet { name: string;}
model Cat is Pet { meow: boolean;}Inheritance with a discriminator for polymorphism:
@discriminator("kind")model Pet { kind: string; name: string;}
model Cat extends Pet { kind: "cat"; meow: boolean;}
model Dog extends Pet { kind: "dog"; bark: boolean;}Suppression
Section titled “Suppression”Suppress when inheritance is genuinely required; otherwise prefer model is or spread (...) instead of extends.