We don’t get a lot of information using typeof
in JavaScript or TypeScript. At most, it tells us whether a value is undefined
, number
, string
, or object
. If you want to get the class name using a function, you can use the following extended version of classOf
that is originally from “JavaScript: The Definitive Guide”:
function classOf(obj: unknown) {
if (obj === null) return "Null";
if (typeof obj === "undefined") return "Undefined";
if (typeof obj === 'object') {
// Warning: this won't work if your Typescript is minified and class names are mangled.
return (obj as object).constructor.name;
}
if (typeof obj === 'function') {
// Warning: this won't work if your Typescript is minified and function names are mangled.
const possibleName = (obj as CallableFunction).name;
if (possibleName) {
return possibleName;
}
}
return Object.prototype.toString.call(obj).slice(8, -1);
}
console.log(classOf(null)); // "Null"
console.log(classOf(undefined)); // "Undefined"
console.log(classOf(1)); // "Number"
console.log(classOf("foobar")); // "String"
console.log(classOf({})); // "Object"
console.log(classOf([])); // "Array"
console.log(classOf(new Date()))
class Test {}
console.log(classOf(new Test())); // "Test"
function test() {}
console.log(classOf(test)); // "test"
console.log(classOf(function () {})) // "Function"
This classOf
function works for any value, including numbers, strings, booleans, classes, and functions. It will return the best name for that given value that is passed. If the object has a constructor name, that value will be returned. If that value is a function, the name of that function will be returned.
Takeaways
Unfortunately, during some transpilation steps or code compression, these names get mangled or removed completely, so it’s best not to rely on the class’s actual name directly. Instead instanceof
should be used, or a static name attribute that cannot be modified.
Top comments (0)