Skip to content

Document authoring mixins in TypeScript #829

Closed
@web-padawan

Description

@web-padawan

I have been looking for a small example of how to create a mixin, but there wasn't one.

So far I was able to come up with the following snippet:

import { LitElement, property } from 'lit-element';

type Constructor<T = object> = {
  new (...args: any[]): T;
  prototype: T;
};

export interface ReadonlyInterface {
  readonly: boolean;
}

export const ReadonlyMixin = <T extends Constructor<LitElement>>(
  base: T
): T & Constructor<ReadonlyInterface> => {
  class ReadonlyMixin extends base implements ReadonlyInterface {
    @property({ type: Boolean }) readonly: boolean = false;
  }
  return ReadonlyMixin;
};

Some things that I learned before I was able to make it work:

Overall it was a bit challenging to figure that out 😅 So the example would help a lot.

@justinfagnani @cdata can you please confirm if the code snippet above is correct? Also, do you have any other guidelines about how mixins should be written in TypeScript?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions