Skip to main content
Version: Next

unref

Callable

  • unref<V>(value): V | undefined
  • unref<T>(value): T
  • unref<T>(value): T | null
  • unref<T>(value): T | undefined
  • unref<T>(value): T | null | undefined

  • Unwraps a reference to its underlying value, returning the entity or scalar. Works on any of:

    • Ref<T> / Reference<T> (entity) — calls .unwrap() to extract the wrapped entity, returns T.
    • LazyRef<T> — type-only marker; runtime value is already T, so returned as-is.
    • plain entity T — returned as-is.
    • ScalarReference<V> / ScalarRef<V> — calls .unwrap() to extract the scalar value, returns V | undefined (a scalar reference may be bound without an initial value).

    Inverse of the ref helper. Use as a typed escape hatch when you know a relation is populated but don't want to (or can't) thread Loaded<T, 'relation'> through a function signature.

    function logAuthor(book: Book) {
    // `book.author` is typed as `LazyRef<Author>`, so `.name` is not directly accessible
    console.log(unref(book.author).name);
    }

    // Scalar reference:
    const scalar = ref('hello');
    unref(scalar); // 'hello' (type: string | undefined)

    Note: unref is a compile-time narrowing only for entity refs. On a LazyRef<T> or plain entity relation that is a stub (PK only, never populated), reads of non-PK properties will still return undefined at runtime — same footgun as a plain non-Ref relation.


    Parameters

    Returns V | undefined