Hyperlinkv0.8.0-beta.28

Schema

Schema.brandfunctioneffect/Schema.ts:5078
brand<S, B>

Adds a nominal brand to a schema, intersecting the output type with Brand.Brand<B> to prevent accidental mixing of structurally identical types.

When to use

Use to make values decoded by an existing schema nominally distinct when the schema already carries the runtime validation you need.

Gotchas

brand adds brand metadata and narrows the TypeScript output type, but it does not add runtime checks.

brandingfromBrand
Source effect/Schema.ts:507846 lines
export interface brand<S extends Constraint, B> extends
  BottomLazy<
    S["ast"],
    brand<S, B>,
    S["~type.parameters"],
    S["~type.mutability"],
    S["~type.optionality"],
    S["~type.constructor.default"],
    S["~encoded.mutability"],
    S["~encoded.optionality"]
  >
{
  readonly "Type": S["Type"] & DistributeBrands<B>
  readonly "Encoded": S["Encoded"]
  readonly "DecodingServices": S["DecodingServices"]
  readonly "EncodingServices": S["EncodingServices"]
  readonly "~type.make.in": S["~type.make.in"]
  readonly "~type.make": S["Type"] & DistributeBrands<B>
  readonly "Iso": S["Type"] & DistributeBrands<B>
  readonly schema: S
  readonly identifier: string
}

/**
 * Adds a nominal brand to a schema, intersecting the output type with
 * `Brand.Brand<B>` to prevent accidental mixing of structurally identical types.
 *
 * **When to use**
 *
 * Use to make values decoded by an existing schema nominally distinct when the
 * schema already carries the runtime validation you need.
 *
 * **Gotchas**
 *
 * `brand` adds brand metadata and narrows the TypeScript output type, but it
 * does not add runtime checks.
 *
 * @see {@link fromBrand} for applying a Brand constructor's checks along with the brand tag
 *
 * @category branding
 * @since 3.10.0
 */
export function brand<B extends string>(identifier: B) {
  return <S extends ConstraintRebuildable>(schema: S): brand<S["Rebuild"], B> =>
    make(SchemaAST.brand(schema.ast, identifier), { schema, identifier })
}
Referenced by 1 symbols