Skip to content

Instantly share code, notes, and snippets.

@disco0
Last active October 5, 2020 10:00
Show Gist options
  • Save disco0/b0fe5af952d96371f4b315eeb381ae95 to your computer and use it in GitHub Desktop.
Save disco0/b0fe5af952d96371f4b315eeb381ae95 to your computer and use it in GitHub Desktop.
TypeScript - Mixin Class
type Constructor<T = {}> = new (...args: any[]) => T;
//#region Mixins
// A mixin that adds a property
function Timestampable<TBase extends Constructor>(Base: TBase)
{
return class extends Base
{
timestamp = Date.now();
};
}
// a mixin that adds a property and methods
function Activatable<TBase extends Constructor>(Base: TBase)
{
return class extends Base
{
isActivated = false;
activate()
{
this.isActivated = true;
}
deactivate()
{
this.isActivated = false;
}
};
}
//#endregion Mixins
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment