API reference › @evolu/common › Type › objectTag
Call Signature
function objectTag<Name extends keyof ObjectTagOutputByName>(
name: ValidateConcreteTypeName<Name>,
): Type<
Name,
ObjectTagOutputByName[Name],
ObjectTagOutputByName[Name],
ObjectTagError<Name>,
null,
ObjectTagError<Name>,
never,
ObjectTagOutputByName[Name]
>;
Defined in: packages/common/src/Type.ts:3913
Realm-neutral Type trusting an object's reported tag.
Predefined tags for JavaScript built-ins expose their native Output type under the assumption that trusted code does not forge their tags. They do not verify native internal slots. A custom tag refines the supplied Type and adds nominal evidence to its Output, so only a value validated by the resulting Type is accepted by its typed operations.
Object.prototype.toString recognizes legitimate JavaScript built-ins from
another realm, but any object can customize the result with
Symbol.toStringTag. Types returned by this factory therefore classify
trusted values; they are not security boundaries. Passing a forged JavaScript
built-in tag violates the trust assumption of the predefined Type. Primitive
Outputs are rejected at compile time.
Example
import { assertOk, objectTag } from "@evolu/common";
const DateType = objectTag("Date");
const date = new Date("2025-01-01T00:00:00.000Z");
assertOk(DateType.fromUnknown(date), date);
Call Signature
function objectTag<
Name extends string,
OutputType extends AnyType & {
Output: object;
},
>(
name: ValidateConcreteTypeName<Name>,
outputType: ValidateOutput<OutputType> &
([
"ObjectTag" extends InferErrors<OutputType>["type"]
? "⛔ Type error: Error type must not duplicate an error inherited from the parent Type."
: never,
] extends [never]
? unknown
: "ObjectTag" extends InferErrors<OutputType>["type"]
? "⛔ Type error: Error type must not duplicate an error inherited from the parent Type."
: never),
): ObjectTagType<Name, OutputType>;
Defined in: packages/common/src/Type.ts:3962
Creates an object-tag Type by refining an existing object Type.
Example
import {
assertEqual,
assertOk,
assertSame,
assertType,
instanceOf,
objectTag,
} from "@evolu/common";
class TaggedValue {
readonly [Symbol.toStringTag] = "TaggedValue";
}
const TaggedValueType = objectTag("TaggedValue", instanceOf(TaggedValue));
const value = new TaggedValue();
const result = TaggedValueType.fromUnknown(value);
assertOk(result);
assertSame(result.value, value);
assertType<typeof result.value extends TaggedValue ? true : false, true>();
assertEqual(TaggedValueType.expected, "TaggedValue");