The or() Function
Tip
You can nest t.or
calls. This can be useful when creating custom types.
{ age: t.or(t.str, t.or(t.int8, t.or(t.date, t.float64, t.none)))}
Tip
You can pass an array or an object to t.or
by using t.schema()
{ age: t.or(t.schema([t.str]), t.int8)}
See more details about t.schema()
Overview
t.or(...types: TCustomType[]): TCustomType
- The
t.or()
function lets you enumerate several serializable types. t.or()
can accept up to 255 arguments.- You cannot directly pass arrays or objects to
t.or
; you must uset.schema()
.
Usage Examples
import { Serializer, t } from "encodexx"
const serializer = new Serializer({ ages: [t.or(t.uint8, t.str)], message: t.or(t.str, t.none)})
serializer.encode({ ages: [12, "493", "not a number", 124, 200], message: null});
import { Serializer, t } from "encodexx"const serializer = new Serializer({ users: [ t.or( t.schema({ name: t.str }), t.schema({ content: t.str, age: t.uint8 }) ), ],});
serializer.encode({ users: [ { name: "ad" }, { content: "content", age: 15 }, { name: "another" }, ]});