Skip to main content

Ref

This page discusses how ref works.

Type Checks

info
  1. The key of Record<string, T> is always non-numeric string.
  2. The key of NumericKeyRecord<T> is always numeric string.
  3. NumericKeyRecord<T> is Record<${number}, T> under the hood.
import {
MetaTypeCreator,
ServerTimestamp,
NumericKeyRecord,
Removable,
createRef,
getDatabase,
} from 'firesagejs'

export type Example = MetaTypeCreator<{
b:
| {
c: boolean | Removable
d: {
e: ServerTimestamp
}
}
| Removable
f: Record<string, 'a' | 'b' | 'c'>
i: NumericKeyRecord<boolean>
}>

const exampleRef = createRef<Example>(getDatabase()) // firesage ref

exampleRef() // point to 'root' node, type is --too long, skipped--
exampleRef('b') // point to 'b' node, type is --too long, skipped--
exampleRef('b/d/e') // point to 'b/d/e' node, write type is ServerTimestamp, compare and read type is number
exampleRef('f') // point to 'f' node, type is Record<string, { h: number; j: { k: boolean } }>
exampleRef('f/abc') // point to 'f/abc' node, type is { h: number; j: { k: boolean } }
exampleRef('f/x1') // alphanumeric key is considered as non-numeric key
exampleRef('i') // point to 'i' node, type is Record<`${number}`, string>
exampleRef('i/123') // point to 'i/123' node, type is string

exampleRef('b/c/d') // not ok, path not exist
exampleRef('f/123') // not ok, expect non-numeric key because f is Record<string, 'a' | 'b' | 'c'>
exampleRef('i/abc') // not ok, expect numeric key because i is NumericKeyRecord<boolean>
ref type checks

Understanding Error Messages

path not exist

expect non-numeric key

note

Numeric key check error message is generic(not quite good), custom error message would be better.

However this is not something that could be easily improved because of complexity, will give this deeper thoughts in the future.

expect numeric key