CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-ts-toolbelt

TypeScript's largest utility library providing 200+ type utilities for advanced type manipulation.

Pending
Overview
Eval results
Files

boolean.mddocs/

Boolean Module

Type-level boolean logic operations using lookup tables for AND, OR, NOT, and XOR operations with 0/1 representation.

Capabilities

Boolean Type System

The Boolean module uses a numeric representation where 0 represents false and 1 represents true.

/**
 * Boolean type represented as 0 (false) or 1 (true)
 */
type Boolean = 0 | 1;

Logical Operations

Core boolean logic operations implemented as mapped types with lookup tables.

/**
 * Logical NOT operation
 * @param B - Boolean value to negate
 * @returns 1 if B is 0, 0 if B is 1
 */
type Not<B extends Boolean> = B extends 0 ? 1 : 0;

/**
 * Logical AND operation using lookup table
 * @param B1 - First boolean value
 * @param B2 - Second boolean value
 * @returns 1 if both B1 and B2 are 1, 0 otherwise
 */
type And<B1 extends Boolean, B2 extends Boolean> = B1 extends 1 ? B2 : 0;

/**
 * Logical OR operation using lookup table
 * @param B1 - First boolean value
 * @param B2 - Second boolean value
 * @returns 1 if either B1 or B2 is 1, 0 if both are 0
 */
type Or<B1 extends Boolean, B2 extends Boolean> = B1 extends 1 ? 1 : B2;

/**
 * Logical XOR (exclusive OR) operation using lookup table
 * @param B1 - First boolean value
 * @param B2 - Second boolean value
 * @returns 1 if B1 and B2 are different, 0 if they are the same
 */
type Xor<B1 extends Boolean, B2 extends Boolean> = B1 extends B2 ? 0 : 1;

Usage Examples:

import { B } from "ts-toolbelt";

// NOT operations
type NotTrue = B.Not<1>; // 0
type NotFalse = B.Not<0>; // 1

// AND operations
type AndTrueTrue = B.And<1, 1>; // 1
type AndTrueFalse = B.And<1, 0>; // 0
type AndFalseTrue = B.And<0, 1>; // 0
type AndFalseFalse = B.And<0, 0>; // 0

// OR operations
type OrTrueTrue = B.Or<1, 1>; // 1
type OrTrueFalse = B.Or<1, 0>; // 1
type OrFalseTrue = B.Or<0, 1>; // 1
type OrFalseFalse = B.Or<0, 0>; // 0

// XOR operations
type XorTrueTrue = B.Xor<1, 1>; // 0
type XorTrueFalse = B.Xor<1, 0>; // 1
type XorFalseTrue = B.Xor<0, 1>; // 1
type XorFalseFalse = B.Xor<0, 0>; // 0

Complex Boolean Expressions

Combine multiple boolean operations to create complex logical expressions.

Usage Examples:

import { B } from "ts-toolbelt";

// Complex expressions using nested operations
type ComplexAnd = B.And<B.Or<1, 0>, B.Not<0>>; // B.And<1, 1> = 1
type ComplexOr = B.Or<B.And<0, 1>, B.Xor<1, 0>>; // B.Or<0, 1> = 1
type ComplexXor = B.Xor<B.And<1, 1>, B.Or<0, 0>>; // B.Xor<1, 0> = 1

// De Morgan's laws demonstration
type A = 1;
type B_Val = 0;

// !(A && B) === (!A || !B)
type NotAndAB = B.Not<B.And<A, B_Val>>; // B.Not<0> = 1
type NotAOrNotB = B.Or<B.Not<A>, B.Not<B_Val>>; // B.Or<0, 1> = 1
// Both equal 1, proving De Morgan's law

// !(A || B) === (!A && !B)
type NotOrAB = B.Not<B.Or<A, B_Val>>; // B.Not<1> = 0
type NotAAndNotB = B.And<B.Not<A>, B.Not<B_Val>>; // B.And<0, 1> = 0
// Both equal 0, proving De Morgan's law

// Truth table validation
type TruthTable = {
  and: [
    B.And<0, 0>, // 0
    B.And<0, 1>, // 0
    B.And<1, 0>, // 0
    B.And<1, 1>  // 1
  ];
  or: [
    B.Or<0, 0>, // 0
    B.Or<0, 1>, // 1
    B.Or<1, 0>, // 1
    B.Or<1, 1>  // 1
  ];
  xor: [
    B.Xor<0, 0>, // 0
    B.Xor<0, 1>, // 1
    B.Xor<1, 0>, // 1
    B.Xor<1, 1>  // 0
  ];
};

Practical Applications

Real-world usage patterns for boolean operations in type-level programming.

Usage Examples:

import { B, A } from "ts-toolbelt";

// Conditional type logic
type IsString<T> = T extends string ? 1 : 0;
type IsNumber<T> = T extends number ? 1 : 0;

type IsStringOrNumber<T> = B.Or<IsString<T>, IsNumber<T>>;
type IsStringAndNumber<T> = B.And<IsString<T>, IsNumber<T>>; // Always 0

type Test1 = IsStringOrNumber<string>; // 1
type Test2 = IsStringOrNumber<number>; // 1
type Test3 = IsStringOrNumber<boolean>; // 0
type Test4 = IsStringAndNumber<string>; // 0

// Flag combination
type HasPermission<Read extends Boolean, Write extends Boolean, Execute extends Boolean> = {
  canRead: Read;
  canWrite: Write;
  canExecute: Execute;
  canReadWrite: B.And<Read, Write>;
  canReadExecute: B.And<Read, Execute>;
  canWriteExecute: B.And<Write, Execute>;
  hasAnyPermission: B.Or<Read, B.Or<Write, Execute>>;
  hasAllPermissions: B.And<Read, B.And<Write, Execute>>;
};

type AdminPermissions = HasPermission<1, 1, 1>;
type ReadOnlyPermissions = HasPermission<1, 0, 0>;
type NoPermissions = HasPermission<0, 0, 0>;

// Validation logic
type IsValidEmail<T> = T extends `${string}@${string}.${string}` ? 1 : 0;
type IsValidLength<T> = T extends string ? (T['length'] extends 0 ? 0 : 1) : 0;

type IsValidEmailAddress<T> = B.And<IsValidEmail<T>, IsValidLength<T>>;

type EmailTest1 = IsValidEmailAddress<"user@example.com">; // 1
type EmailTest2 = IsValidEmailAddress<"invalid-email">; // 0
type EmailTest3 = IsValidEmailAddress<"">; // 0

// State machine logic
type StateTransition<
  CurrentState extends Boolean,
  Input extends Boolean,
  TransitionCondition extends Boolean
> = B.And<CurrentState, B.And<Input, TransitionCondition>>;

type State1 = 0; // Initial state (off)
type Input1 = 1; // Turn on signal
type CanTransition = 1; // Transition allowed

type NewState = StateTransition<State1, Input1, CanTransition>; // 0 (stays off due to current state)

Types

// Core boolean type using numeric representation
type Boolean = 0 | 1;

Install with Tessl CLI

npx tessl i tessl/npm-ts-toolbelt

docs

any.md

boolean.md

class.md

community.md

function.md

index.md

iteration.md

list.md

misc.md

number.md

object.md

string.md

testing.md

union.md

tile.json