Skip to content

Instantly share code, notes, and snippets.

@FrameMuse
Created March 2, 2026 18:07
Show Gist options
  • Select an option

  • Save FrameMuse/8d1090c122b98f5c5c813e8558fff9d2 to your computer and use it in GitHub Desktop.

Select an option

Save FrameMuse/8d1090c122b98f5c5c813e8558fff9d2 to your computer and use it in GitHub Desktop.
Class Mixin in TypeScript with `instanceof` override

Example

class Person {
  name!: string
  age!: number

  private gender!: "aircraft"
}

class Profile {
  name!: string
  avatar!: string

  protected something!: {}
}

export class User extends mixin(Person, Profile) {
  id!: number

  asd() {
    this.gender // => Error: Property is private
    this.something
  }
}


const user = new User
user.gender // => Error: Property is private
user.something // => Error: Property is protected

user instanceof User // === true
user instanceof Person // === true
user instanceof Profile // === true
user instanceof mixin(Person, Profile) // === true

Example Notes

  • a mixin class can define public/protected/private property visibility.
  • a mixin class can't define a constructor
  • mixin classes must match or error will be shown at mixin(Person, Profile).
  • a class that extends a mixin can be checked with instanceof for each class and altogether.

Type per class

When inspecting a property from a mixin, your IDE will display what this type belongs to. image

Type super

When inspecting super, your IDE will display the mixins.

image
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment