tguard-js
    Preparing search index...

    Function getType

    • Returns the type of the provided value as a string.

      Parameters

      • x: unknown

        The value whose type is to be determined.

      • OptionalnameOnly: boolean

        Whether to return a simplified name instead of a full tag.

      Returns string

      A string representing the type of the value.

      This function provides a normalized type representation by combining typeof and Object.prototype.toString semantics.

      • For null, it returns 'null' (instead of the misleading 'object' from typeof).
      • For primitives (string, number, boolean, bigint, symbol, undefined), it returns the result of typeof.
      • For objects and built-in instances, it returns the internal tag (e.g., '[object Date]', '[object Array]').
      • For functions and classes:
        • When nameOnly is false, it returns 'function'.
        • When nameOnly is true, it attempts to return the function or class name.

      When nameOnly is true, the function attempts to resolve a meaningful name:

      1. Uses fn.name if available (including inferred names).
      2. Falls back to parsing Function.prototype.toString.
      3. Returns a fallback string if no name can be determined.
      Value getType(x) getType(x, true)
      null '[object Null]' 'null'
      undefined 'undefined' 'undefined'
      'text' 'string' 'string'
      123 'number' 'number'
      123n 'bigint' 'bigint'
      [] '[object Array]' 'Array'
      {} '[object Object]' 'Object'
      new Date() '[object Date]' 'Date'
      function foo(){} 'function' 'foo'
      const x = () => {} 'function' 'x'
      (() => {}) 'function' '(anonymous)'
      class MyClass {} 'function' 'MyClass'
      class {} 'function' '(anonymous)'
      function() {} 'function' '(anonymous)'
      • Function and class names may be inferred from variable bindings:

        const fn = () => {};
        fn.name === 'fn';
      • Anonymous functions or classes (without bindings nor variable names) may not have a name, and will return '(anonymous)' as a fallback.

      • In minified or transpiled code, names may be altered or removed.

      1.0