Get type of a variable in string
JavaScript version
const getTypeOf = (obj) => Object.prototype.toString.call(obj).match(/\[object (.*)\]/)[1];
TypeScript version
const getTypeOf = (obj: any): string => (Object.prototype.toString.call(obj).match(/\[object (.*)\]/) as string[])[1];
Examples
getTypeOf('hello world');
getTypeOf(1000);
getTypeOf(Infinity);
getTypeOf(true);
getTypeOf(Symbol());
getTypeOf(null);
getTypeOf(undefined);
getTypeOf({});
getTypeOf([]);
getTypeOf(/[a-z]/g);
getTypeOf(new Date(2021));
getTypeOf(new Error());
getTypeOf(function () {});
getTypeOf((a, b) => a + b);
getTypeOf(async () => {});
getTypeOf(document);