Skip to content

Type Detection - getTypeOf

getTypeOf returns the type name of any value as a string. It works reliably everywhere in Google Apps Script, including across script boundaries where instanceof fails silently.

Uses Object.prototype.toString and Symbol.toStringTag for safe, standard-compliant type detection that works in all execution contexts.

JavaScript
gaslamp.getTypeOf("hello");     // "string"
gaslamp.getTypeOf([1, 2, 3]);   // "array"
gaslamp.getTypeOf(new Date());  // "date"

Without gaslamp

Checking object types with instanceof is the standard approach:

JavaScript
const value = LibraryFunction.getData();

if (value instanceof Date) {
  console.log("date");
} else if (value instanceof Array) {
  console.log("array");
} else if (value instanceof Map) {
  console.log("map");
}

The problem: instanceof fails silently when the object crosses a script boundary (e.g., from a library script or external source). The code treats valid objects as invalid types.

This is a known GAS limitation. Objects created in different execution contexts don't share the same constructor reference, so instanceof returns false.


With gaslamp

getTypeOf uses Object.prototype.toString and Symbol.toStringTag, which work across script boundaries:

JavaScript
const value = LibraryFunction.getData();
const type = gaslamp.getTypeOf(value);

if (type === "date") {
  console.log("date");
} else if (type === "array") {
  console.log("array");
} else if (type === "map") {
  console.log("map");
} else if (type === "bareframe") {
  console.log("It's a BareFrame!");
}

The returned type string is always accurate, regardless of script boundaries. This includes gaslamp's own classes (BareFrame, FlameFrame, GroupedFrame, etc.), which use Symbol.toStringTag for reliable cross-boundary type detection.


Supported Type Names

Standard JavaScript types

JavaScript
gaslamp.getTypeOf("hello");        // "string"
gaslamp.getTypeOf(42);             // "number"
gaslamp.getTypeOf(true);           // "boolean"
gaslamp.getTypeOf(null);           // "null"
gaslamp.getTypeOf(undefined);      // "undefined"
gaslamp.getTypeOf([1, 2, 3]);      // "array"
gaslamp.getTypeOf(new Date());     // "date"
gaslamp.getTypeOf(new Map());      // "map"
gaslamp.getTypeOf(new Set());      // "set"
gaslamp.getTypeOf(/regex/);        // "regexp"
gaslamp.getTypeOf({ a: 1 });       // "object"
gaslamp.getTypeOf(function() {}); // "function"

gaslamp DataFrame types

JavaScript
// BareFrame — schema-free DataFrame
const df = gaslamp.BareFrame.fromColumns({ name: ["Alice", "Bob"], age: [25, 30] });
gaslamp.getTypeOf(df);             // "bareframe"

// FlameFrame — schema-validated DataFrame
const { passed } = gaslamp.FlameFrame.from(df, schema);
gaslamp.getTypeOf(passed);         // "flameframe"

// GroupedFrame — grouped rows from df.groupBy()
const grouped = df.groupBy(["name"]);
gaslamp.getTypeOf(grouped);        // "groupedframe"

// LazyFrame — lazy filter evaluation
const lazy = gaslamp.LazyFrame.from(df).filter(expr);
gaslamp.getTypeOf(lazy);           // "lazyframe"

// ForgeFrame — statistical histogram analysis
const forge = gaslamp.ForgeFrame.from(df);
gaslamp.getTypeOf(forge);          // "forgeframe"

// Expression — filter predicate builder
const expr = gaslamp.Expression.col("age").gt(18);
gaslamp.getTypeOf(expr);           // "expression"

Sheet cells often contain mixed types. getTypeOf helps distinguish between them: "string" for text, "number" for numeric cells, "date" for formatted dates. Use gaslamp frame types when working with DataFrame results.


Type Branching with Switch

Use getTypeOf when you need to handle multiple types:

JavaScript
function processValue(value) {
  switch (gaslamp.getTypeOf(value)) {
    case "date":
      return Utilities.formatDate(value, "America/New_York", "yyyy-MM-dd");
    case "number":
      return Math.round(value * 100) / 100;
    case "string":
      return value.trim().toLowerCase();
    case "array":
      return value.join(", ");
    default:
      return String(value);
  }
}

Comparison: getTypeOf vs FlameGuards

For validating a single type, prefer FlameGuards — they return a boolean and work with error messages:

JavaScript
// Use FlameGuards for validation (returns true/false)
if (gaslamp.FlameGuards.isValidDate(value)) {
  // Guaranteed to be a Date
}

// Use getTypeOf for type names (returns string)
if (gaslamp.getTypeOf(value) === "date") {
  // You know it's a Date
}

// Use getValuePreviewOf for readable value display (for logging)
console.log(`Got: ${gaslamp.getValuePreviewOf(value)}`);
// Got: "2024-01-15"  (quoted string)
// Got: map: {"a" => 1}  (Map display)

// Works with gaslamp frame types too
const df = gaslamp.BareFrame.fromSheet(sheet);
if (gaslamp.getTypeOf(df) === "bareframe") {
  console.log("Successfully loaded a BareFrame");
}
Scenario Use This Returns
Validate one type FlameGuards.isValidDate(value) boolean
Validate multiple types FlameWright.anyOf([...]) boolean
Get a type name getTypeOf(value) string ("date", "array", "bareframe", etc.)
Get readable value preview getValuePreviewOf(value) string (human-readable)
Type branching with switch/if-else getTypeOf(value) string

Next Steps