Javascript Question

How to check if a variable is an ES6 class declaration?

I am exporting the following ES6 class from one module:

export class Thingy {
hello() {
console.log("A");
}

world() {
console.log("B");
}
}


And importing it from another module:

import {Thingy} from "thingy";

if (isClass(Thingy)) {
// Do something...
}


How can I check whether a variable is a class? Not a class instance, but a class declaration?

In other words, how would I implement the
isClass
function in the example above?

Answer

In ES6, there is no "class" when code is running, you just have a constructor function, or an object. So you could do

if (typeof Thingy === 'function'){
  // It's a function, so it definitely can't be an instance.
} else {
  // It could be anything other than a constructor
}

As long as you know for a fact that something is either an instance or a constructor.