跳到主要内容

getType

TS JS Deno

Returns the native type of a value.

Return 'undefined' or 'null' if the value is undefined or null. Otherwise, use Object.prototype.constructor.name to get the name of the constructor.

typescript
const getType = (v: any) =>
v === undefined
? "undefined"
: v === null
? "null"
: v.constructor.name.toLowerCase();
typescript
getType(new Set([1, 2, 3])); // 'set'
getType(null); // 'null'
getType(Deno), "object";