variable / @typed/guard

addTag

Adds a readonly _tag to every matched object output. The output must not already have an _tag property.

Package version
1.0.0-beta.13
Category
Record construction
Since
1.0.0

Import

import { addTag } from "@typed/guard";

Signatures

export declare const addTag: {
    <B>(value: B): <G extends GuardInput<any, any, any, any>>(guard: G & RecordOutputConstraint<NoInfer<Guard.Output<G>>> & ("_tag" extends NoInfer<Guard.Output<G> extends infer O ? (O extends unknown ? keyof O : never) : never> ? never : unknown)) => Guard<Guard.Input<G>, Guard.Output<G> & {
        readonly _tag: B;
    }, Guard.Error<G>, Guard.Services<G>>;
    <G extends GuardInput<any, any, any, any>, B>(guard: G & RecordOutputConstraint<NoInfer<Guard.Output<G>>> & ("_tag" extends NoInfer<Guard.Output<G> extends infer O ? (O extends unknown ? keyof O : never) : never> ? never : unknown), value: B): Guard<Guard.Input<G>, Guard.Output<G> & {
        readonly _tag: B;
    }, Guard.Error<G>, Guard.Services<G>>;
};

Why

A typed discriminant turns matched record outputs into exhaustively narrowable tagged values while rejecting key collisions.

Ownership and lifetime

This pure combinator acquires no resources and creates a fresh plain object for every match.

Examples

import { addTag, bindTo, liftPredicate } from "@typed/guard"
const tagged = addTag(bindTo(liftPredicate(Boolean), "value"), "Input")

Source