Skip to content

Instantly share code, notes, and snippets.

@web-padawan
Last active November 10, 2020 17:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save web-padawan/a05ef31259680c092d3cca5dee49b976 to your computer and use it in GitHub Desktop.
Save web-padawan/a05ef31259680c092d3cca5dee49b976 to your computer and use it in GitHub Desktop.
const $readonly = Symbol('readonly');
type Constructor<T = object> = {
new (...args: any[]): T;
prototype: T;
};
// mixin interface
interface ReadonlyInterface {
readonly: boolean;
}
// mixin class
export const ReadonlyMixin = <T extends Constructor<HTMLElement>>(
base: T
): T & Constructor<ReadonlyInterface> => {
class HasReadonly extends base {
private [$readonly]!: boolean;
set readonly(readonly: boolean) {
this[$readonly] = readonly;
}
get readonly(): boolean {
return this[$readonly];
}
}
return HasReadonly;
};
// using a mixin
class MyElement extends ReadonlyMixin(HTMLElement) {
constructor() {
super();
this.readonly = true;
}
}
customElements.define('my-element', MyElement);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment