BrandApp Playground

Type Coercion

Safely extract and convert EAV record values to specific TypeScript types. Single-argument helpers return undefined for missing/invalid input — pair them with ?? default; asEnum takes an allowed-set and a fallback.

Try It: Type any value to see coercion results

Enter a value below and see how each coercion function interprets it in real-time. All functions run client-side with no API calls.

Example values — click to try

FunctionReturn TypeResult
asString()string | undefined"42"string
asNumber()number | undefined42number
asBoolean()boolean | undefinedundefined
asDate()Date | undefined2042-01-01T00:00:00.000ZDate
asJson()T | undefined42number
asStringArray()string[] | undefined["42"]string[]
asEnum()"active" | "inactive" | "pending""active"string

Typing "null" passes actual null to the functions. All other inputs are passed as strings, matching how EAV record values arrive from the server.

asString()

Convert a value to a string. Returns undefined if the value is null/undefined.

example.ts
import { asString } from "@reopt-ai/brandapp-sdk/eav/coerce";

const record = await eav.records.get(entityId, recordId);

// Safely extract string values
const name = asString(record.values.name);        // string | undefined
const name2 = asString(record.values.name) ?? ""; // string (with default)

asNumber()

Convert a value to a number. Returns undefined for invalid numbers.

example.ts
import { asNumber } from "@reopt-ai/brandapp-sdk/eav/coerce";

// Convert to number, returns undefined if not a valid number
const age = asNumber(record.values.age);       // number | undefined
const age2 = asNumber(record.values.age) ?? 0; // number (with default)

// Works with string representations
asNumber("42");    // 42
asNumber("abc");   // undefined
asNumber(null);    // undefined

asBoolean()

Convert a value to a boolean with common truthy/falsy string handling.

example.ts
import { asBoolean } from "@reopt-ai/brandapp-sdk/eav/coerce";

// Convert to boolean
const active = asBoolean(record.values.active);           // boolean | undefined
const active2 = asBoolean(record.values.active) ?? false; // boolean (default)

// Truthy values: true, "true", "1", 1
// Falsy values: false, "false", "0", 0

asDate()

Convert a value to a Date object from ISO strings or timestamps.

example.ts
import { asDate } from "@reopt-ai/brandapp-sdk/eav/coerce";

// Convert to Date object
const created = asDate(record.values.createdAt);  // Date | undefined

// Works with ISO strings and timestamps
asDate("2024-01-15T10:30:00Z");  // Date
asDate(1705312200000);            // Date
asDate("invalid");                // undefined

asJson()

Parse JSON values stored in EAV fields with type inference.

example.ts
import { asJson } from "@reopt-ai/brandapp-sdk/eav/coerce";

// Parse JSON stored in EAV values
type Address = { street: string; city: string };

const address = asJson<Address>(record.values.address);
// Address | undefined

// With default value via nullish coalescing
const address2 = asJson<Address>(record.values.address) ?? {
  street: "",
  city: "",
};

asStringArray()

Coerce multiselect / JSON / single values into a string[].

example.ts
import { asStringArray } from "@reopt-ai/brandapp-sdk/eav/coerce";

// Coerce multiselect / array values to a string[]
const tags = asStringArray(record.values.tags); // string[] | undefined
const tags2 = asStringArray(record.values.tags) ?? [];

asStringArray(["a", "b"]);   // ["a", "b"]
asStringArray('["a","b"]');  // ["a", "b"]  (JSON string)
asStringArray("solo");       // ["solo"]    (non-JSON → single element)
asStringArray(null);         // undefined

asEnum() & asEnumOrUndefined()

Constrain a value to a fixed allowed set — with a fallback (asEnum) or returning undefined for non-members (asEnumOrUndefined).

example.ts
import {
  asEnum,
  asEnumOrUndefined,
} from "@reopt-ai/brandapp-sdk/eav/coerce";

// asEnum(value, allowed, fallback) — always returns a member of `allowed`
const STATUSES = ["active", "inactive", "pending"] as const;
const status = asEnum(record.values.status, STATUSES, "active");
// status: "active" | "inactive" | "pending"

// asEnumOrUndefined(value, allowed) — returns undefined for non-members
const role = asEnumOrUndefined(record.values.role, [
  "admin",
  "member",
  "guest",
] as const);
// role: "admin" | "member" | "guest" | undefined