-
-
Save web-padawan/a05ef31259680c092d3cca5dee49b976 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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