or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

any.mdboolean.mdclass.mdcommunity.mdfunction.mdindex.mditeration.mdlist.mdmisc.mdnumber.mdobject.mdstring.mdtesting.mdunion.md

boolean.mddocs/

0

# Boolean Module

1

2

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

3

4

## Capabilities

5

6

### Boolean Type System

7

8

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

9

10

```typescript { .api }

11

/**

12

* Boolean type represented as 0 (false) or 1 (true)

13

*/

14

type Boolean = 0 | 1;

15

```

16

17

### Logical Operations

18

19

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

20

21

```typescript { .api }

22

/**

23

* Logical NOT operation

24

* @param B - Boolean value to negate

25

* @returns 1 if B is 0, 0 if B is 1

26

*/

27

type Not<B extends Boolean> = B extends 0 ? 1 : 0;

28

29

/**

30

* Logical AND operation using lookup table

31

* @param B1 - First boolean value

32

* @param B2 - Second boolean value

33

* @returns 1 if both B1 and B2 are 1, 0 otherwise

34

*/

35

type And<B1 extends Boolean, B2 extends Boolean> = B1 extends 1 ? B2 : 0;

36

37

/**

38

* Logical OR operation using lookup table

39

* @param B1 - First boolean value

40

* @param B2 - Second boolean value

41

* @returns 1 if either B1 or B2 is 1, 0 if both are 0

42

*/

43

type Or<B1 extends Boolean, B2 extends Boolean> = B1 extends 1 ? 1 : B2;

44

45

/**

46

* Logical XOR (exclusive OR) operation using lookup table

47

* @param B1 - First boolean value

48

* @param B2 - Second boolean value

49

* @returns 1 if B1 and B2 are different, 0 if they are the same

50

*/

51

type Xor<B1 extends Boolean, B2 extends Boolean> = B1 extends B2 ? 0 : 1;

52

```

53

54

**Usage Examples:**

55

56

```typescript

57

import { B } from "ts-toolbelt";

58

59

// NOT operations

60

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

61

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

62

63

// AND operations

64

type AndTrueTrue = B.And<1, 1>; // 1

65

type AndTrueFalse = B.And<1, 0>; // 0

66

type AndFalseTrue = B.And<0, 1>; // 0

67

type AndFalseFalse = B.And<0, 0>; // 0

68

69

// OR operations

70

type OrTrueTrue = B.Or<1, 1>; // 1

71

type OrTrueFalse = B.Or<1, 0>; // 1

72

type OrFalseTrue = B.Or<0, 1>; // 1

73

type OrFalseFalse = B.Or<0, 0>; // 0

74

75

// XOR operations

76

type XorTrueTrue = B.Xor<1, 1>; // 0

77

type XorTrueFalse = B.Xor<1, 0>; // 1

78

type XorFalseTrue = B.Xor<0, 1>; // 1

79

type XorFalseFalse = B.Xor<0, 0>; // 0

80

```

81

82

### Complex Boolean Expressions

83

84

Combine multiple boolean operations to create complex logical expressions.

85

86

**Usage Examples:**

87

88

```typescript

89

import { B } from "ts-toolbelt";

90

91

// Complex expressions using nested operations

92

type ComplexAnd = B.And<B.Or<1, 0>, B.Not<0>>; // B.And<1, 1> = 1

93

type ComplexOr = B.Or<B.And<0, 1>, B.Xor<1, 0>>; // B.Or<0, 1> = 1

94

type ComplexXor = B.Xor<B.And<1, 1>, B.Or<0, 0>>; // B.Xor<1, 0> = 1

95

96

// De Morgan's laws demonstration

97

type A = 1;

98

type B_Val = 0;

99

100

// !(A && B) === (!A || !B)

101

type NotAndAB = B.Not<B.And<A, B_Val>>; // B.Not<0> = 1

102

type NotAOrNotB = B.Or<B.Not<A>, B.Not<B_Val>>; // B.Or<0, 1> = 1

103

// Both equal 1, proving De Morgan's law

104

105

// !(A || B) === (!A && !B)

106

type NotOrAB = B.Not<B.Or<A, B_Val>>; // B.Not<1> = 0

107

type NotAAndNotB = B.And<B.Not<A>, B.Not<B_Val>>; // B.And<0, 1> = 0

108

// Both equal 0, proving De Morgan's law

109

110

// Truth table validation

111

type TruthTable = {

112

and: [

113

B.And<0, 0>, // 0

114

B.And<0, 1>, // 0

115

B.And<1, 0>, // 0

116

B.And<1, 1> // 1

117

];

118

or: [

119

B.Or<0, 0>, // 0

120

B.Or<0, 1>, // 1

121

B.Or<1, 0>, // 1

122

B.Or<1, 1> // 1

123

];

124

xor: [

125

B.Xor<0, 0>, // 0

126

B.Xor<0, 1>, // 1

127

B.Xor<1, 0>, // 1

128

B.Xor<1, 1> // 0

129

];

130

};

131

```

132

133

### Practical Applications

134

135

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

136

137

**Usage Examples:**

138

139

```typescript

140

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

141

142

// Conditional type logic

143

type IsString<T> = T extends string ? 1 : 0;

144

type IsNumber<T> = T extends number ? 1 : 0;

145

146

type IsStringOrNumber<T> = B.Or<IsString<T>, IsNumber<T>>;

147

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

148

149

type Test1 = IsStringOrNumber<string>; // 1

150

type Test2 = IsStringOrNumber<number>; // 1

151

type Test3 = IsStringOrNumber<boolean>; // 0

152

type Test4 = IsStringAndNumber<string>; // 0

153

154

// Flag combination

155

type HasPermission<Read extends Boolean, Write extends Boolean, Execute extends Boolean> = {

156

canRead: Read;

157

canWrite: Write;

158

canExecute: Execute;

159

canReadWrite: B.And<Read, Write>;

160

canReadExecute: B.And<Read, Execute>;

161

canWriteExecute: B.And<Write, Execute>;

162

hasAnyPermission: B.Or<Read, B.Or<Write, Execute>>;

163

hasAllPermissions: B.And<Read, B.And<Write, Execute>>;

164

};

165

166

type AdminPermissions = HasPermission<1, 1, 1>;

167

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

168

type NoPermissions = HasPermission<0, 0, 0>;

169

170

// Validation logic

171

type IsValidEmail<T> = T extends `${string}@${string}.${string}` ? 1 : 0;

172

type IsValidLength<T> = T extends string ? (T['length'] extends 0 ? 0 : 1) : 0;

173

174

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

175

176

type EmailTest1 = IsValidEmailAddress<"user@example.com">; // 1

177

type EmailTest2 = IsValidEmailAddress<"invalid-email">; // 0

178

type EmailTest3 = IsValidEmailAddress<"">; // 0

179

180

// State machine logic

181

type StateTransition<

182

CurrentState extends Boolean,

183

Input extends Boolean,

184

TransitionCondition extends Boolean

185

> = B.And<CurrentState, B.And<Input, TransitionCondition>>;

186

187

type State1 = 0; // Initial state (off)

188

type Input1 = 1; // Turn on signal

189

type CanTransition = 1; // Transition allowed

190

191

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

192

```

193

194

## Types

195

196

```typescript { .api }

197

// Core boolean type using numeric representation

198

type Boolean = 0 | 1;

199

```