Skip to content

Mixin type conflict at runtime

Quick summary

Two mixins define the same method — the last applied mixin wins, and earlier methods are silently lost at runtime.

Why this happens

  • Two mixins both define a method with the same name, and the application order determines which one survives

  • The mixin types do not declare conflicts, so TypeScript does not warn about the overlap

  • A mixin overrides a base class method with incompatible behavior, and the type system does not catch it

Minimal example

✗ Broken code
type Constructor<T> = new (...args: any[]) => T;
function Loggable<T extends Constructor<{}>>(Base: T) {
  return class extends Base { log() { console.log('log'); } };
}
function Traceable<T extends Constructor<{}>>(Base: T) {
  return class extends Base { log() { console.log('trace'); } }; // conflicts!
}
class MyClass extends Traceable(Loggable(Base)) {} // only 'trace' log survives
✓ Fixed code
function Loggable<T extends Constructor<{}>>(Base: T) {
  return class extends Base { log() { console.log('log'); } };
}
function Traceable<T extends Constructor<{}>>(Base: T) {
  return class extends Base { trace() { console.log('trace'); } }; // renamed
}
class MyClass extends Traceable(Loggable(Base)) {} // both log and trace available

Renaming the conflicting method in one mixin avoids the collision, so both methods are available on the final class.

How to diagnose

  • Log the method names on the mixed-in object to see which mixin's version is present

  • Check the application order of mixins and trace which one defines each method last

  • Use Object.getOwnPropertyNames() on each mixin to find overlapping property names

How to fix

  • Rename conflicting methods in one mixin to avoid the collision

  • Use a composed mixin that explicitly calls both mixin methods instead of letting one override the other

  • Add type constraints that detect and report mixin conflicts at compile time

How to prevent

  • Document each mixin's interface and check for method name conflicts before applying multiple mixins

  • Use a mixin composition utility that detects and warns about conflicting property names

Related Resources

Related Glossary

Related Lessons

← Back to language