Created
July 15, 2020 14:17
-
-
Save calebdwilliams/d5ccdcc4aa8a190fd41cc8422af46305 to your computer and use it in GitHub Desktop.
Mixin annotation with JSDoc and TypeScript
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
import { LitElement } from 'https://cdn.pika.dev/lit-element@^2.3.1'; | |
/** @typedef {new (...args: any[]) => any} Constructor */ | |
/** | |
* @template {!Constructor} T | |
* @param {T} superclass - The class to extend | |
*/ | |
const FormControlMixin = (superclass) => | |
class FormControl extends superclass { | |
/** @type {boolean} */ | |
get checkValidity() { | |
return /* some magic */ | |
} | |
} | |
/** | |
* @template FormControlMixin, LitElement | |
*/ | |
class MyInput extends FormControlMixin(LitElement) { | |
/** | |
* TypeScript now recognizes this has checkValidity | |
* as well as all of LitElement's classes | |
*/ | |
} |
I tried it with two mixins, and it didn't work as expected.
Yeah, it’sa bit of I think. I’ll try to update.
That worked for me! Thank you
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Wow, it is so great that TypeScript mixins are supported in plain JS with JSDoc now! Yes!! I think for a while this was not possible without the
{!Constructor}
syntax to enforce that T extends from that.