Skip to content

Instantly share code, notes, and snippets.

@olehermanse
Created February 13, 2024 15:07
Show Gist options
  • Save olehermanse/f2b4bd542492c88f9e12c43f2ca47d35 to your computer and use it in GitHub Desktop.
Save olehermanse/f2b4bd542492c88f9e12c43f2ca47d35 to your computer and use it in GitHub Desktop.
An experiment in trying to determine the "type" of something
class Mock extends Object {
userid: string;
username: string;
}
function type_of_extended(a: any): string[] {
const to = typeof a;
if (["string", "number", "boolean", "null", "undefined"].includes(to)) {
return [to];
}
if (to === "function" && a.prototype) {
if (Object.getOwnPropertyDescriptor(a, "prototype")?.writable) {
return ["function"];
}
return ["class", a.name];
}
// console.log(a.toString());
if (to === "function" && a instanceof Object) {
return ["function"];
}
if (a instanceof Object && a.constructor && a.constructor.name) {
// Instance of a class
return [a.constructor.name, "instance"];
}
return ["Unknown"];
}
function type_of(a: any): string {
return type_of_extended(a)[0];
}
function wrapper(rep: string, actual: any) {
console.log("type_of(" + rep + ") = " + type_of(actual));
}
wrapper("3", 3);
wrapper("'foo'", "foo");
wrapper("true", true);
wrapper("false", false);
wrapper("[]", []);
wrapper("{}", {});
wrapper("wrapper", wrapper);
wrapper("Mock", Mock);
wrapper("Object", Object);
wrapper("Array", Array);
wrapper("new Mock()", new Mock());
// Output:
// type_of(3) = number
// type_of('foo') = string
// type_of(true) = boolean
// type_of(false) = boolean
// type_of([]) = Array
// type_of({}) = Object
// type_of(wrapper) = function
// type_of(Mock) = class
// type_of(Object) = class
// type_of(Array) = class
// type_of(new Mock()) = Mock
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment