Hone logo
Hone
Problems

Type-Level Boolean Logic in TypeScript

This challenge explores the fascinating world of type-level programming in TypeScript. We'll be crafting type aliases that represent boolean values and implementing basic logical operations (AND, OR, NOT) entirely at the type level, without runtime execution. This is useful for enforcing constraints and performing compile-time checks, leading to more robust and predictable code.

Problem Description

The goal is to create a system of type aliases that represent boolean values (true and false) and then implement type-level versions of the logical operators AND, OR, and NOT. These operators should take type arguments representing boolean values and produce a new type representing the result of the logical operation. The core idea is to leverage conditional types and distributive conditional types to achieve this.

What needs to be achieved:

  1. Define type aliases for True and False representing boolean values at the type level.
  2. Implement a type alias And<A extends boolean, B extends boolean> that returns True if both A and B are True, and False otherwise.
  3. Implement a type alias Or<A extends boolean, B extends boolean> that returns True if either A or B is True, and False otherwise.
  4. Implement a type alias Not<A extends boolean> that returns True if A is False, and False if A is True.

Key Requirements:

  • All operations must be performed at the type level. No runtime code should be involved.
  • The resulting types should accurately reflect the boolean logic.
  • The solution should be type-safe and avoid any type errors.

Expected Behavior:

  • And<True, True> should evaluate to True.
  • And<True, False> should evaluate to False.
  • And<False, True> should evaluate to False.
  • And<False, False> should evaluate to False.
  • Or<True, True> should evaluate to True.
  • Or<True, False> should evaluate to True.
  • Or<False, True> should evaluate to True.
  • Or<False, False> should evaluate to False.
  • Not<True> should evaluate to False.
  • Not<False> should evaluate to True.

Edge Cases to Consider:

  • Ensure the types are correctly handled when True or False are used as arguments.
  • Consider how the conditional types behave with different type arguments.

Examples

Example 1:

Input: And<True, True>
Output: True
Explanation: Both inputs are True, so the AND operation results in True.

Example 2:

Input: Or<False, True>
Output: True
Explanation: One of the inputs is True, so the OR operation results in True.

Example 3:

Input: Not<True>
Output: False
Explanation: The input is True, so the NOT operation inverts it to False.

Constraints

  • The solution must be written in TypeScript.
  • The solution should be concise and readable.
  • The solution should not rely on any external libraries or complex type manipulations beyond conditional types and distributive conditional types.
  • The solution must be type-safe and not produce any type errors.

Notes

  • Think about how conditional types (A extends B ? C : D) can be used to represent boolean logic.
  • Distributive conditional types are key to making these operations work correctly with more complex types.
  • Start by defining the True and False types, then build up to the logical operations.
  • Consider using utility types like Infer if needed, but the core logic should be based on conditional types.
Loading editor...
typescript