Skip to content

getObjectRecord

Jason Godesky edited this page Jul 11, 2026 · 1 revision

getObjectRecord returns false if given something that is not an object or the object cast as Record<string, unknown>.

Tip

A common pattern in our type guards for custom interfaces is first to assert that the candidate passes isObject and then to cast it to Record<string, unknown> so that we can test the shape of that object. This method consolidates that into a single step.

Parameters

candidate: unknown

Something that could be an object.

Returns

false if candidate fails isObject, or candidate cast as Record<string, unknown> if it passes.

Examples

import { getObjectRecord, isNumber, isString } from '@revolutionarygamesco/common'

interface TestCase {
  n: number
  label: string
}

const isTestCase = (
  candidate: unknown
}: candidate is TestCase => {
  const record = getObjectRecord(candidate)
  if (!record) return false

  return [
    isNumber(record.n),
    isString(record.label)
  ].every(test => test === true)
}

See also

Clone this wiki locally