Monday 26 October 2020

How to properly annotate a cached mixin

export function Mixin(base) {
  return class extends base {
    foo = "bar";
  }
}

class A extends Mixin(SuperClass) {

}

let a = new A;
a. // suggests foo

This works beautifully, but in my case more often than not SuperClass is the same constructor, so I figured it's a good idea to implement caching.

let cache = new Map();
export function Mixin(base) {
  if (!cache.has(base)) {
    cache.set(base, class extends base {
      foo = "bar";
    })
  }
  return cache.get(base);
}

class A extends Mixin(SuperClass) {

}

a. // no longer suggests foo

What's the proper way to annotate this?



from How to properly annotate a cached mixin

No comments:

Post a Comment