optional<T>(field: XmlField<T>): XmlField<T> & { optional: true; }
Marks a field as possibly missing: it reads as absent instead of throwing, and is not written when absent.
Read a missing optional field
Read a missing optional field
import { assertEquals } from "@std/assert"; import { parse } from "@std/xml"; import { int, optional, xmlParam } from "@hertzg/reolink-api/protocol/xml"; const port = xmlParam("RtmpPort", { rtmpPort: int(), enable: optional(int()) }); const root = parse("<RtmpPort><rtmpPort>1935</rtmpPort></RtmpPort>").root; assertEquals(port.decode(root), { rtmpPort: 1935 });