ts-type-forge
    Preparing search index...

    Type Alias Brand<T, TrueKeys, FalseKeys>

    Brand: T & TSTypeForgeInternals_BrandEncapsulated<
        {
            readonly [key in FalseKeys
            | TrueKeys]: key extends TrueKeys ? true : false
        },
    >

    Creates a branded type (nominal type) to distinguish values at the type level. Branded types prevent accidental type compatibility even when the underlying types are the same.

    Type Parameters

    • T

      The underlying value type to be branded

    • TrueKeys extends string

      String literal keys that will be marked as true in the brand

    • FalseKeys extends string = never

      String literal keys that will be marked as false in the brand (defaults to never)

    // Create distinct ID types
    type UserId = Brand<string, 'UserId'>;
    type PostId = Brand<string, 'PostId'>;

    // These are incompatible even though both are strings
    const userId: UserId = "user123" as UserId;
    const postId: PostId = "post456" as PostId;
    // const wrongAssignment: UserId = postId; // Error!

    // Create validated types
    type NonZeroInt = Brand<number, 'integer', 'zero'>;