TypeScript Type-Level Logic Utilities
This package provides experimental type-level boolean logic primitives using TypeScript's advanced generic system. With a version of `0.0.0` and last published over seven years ago (September 2018), it is in an extremely early, experimental state and is effectively abandoned. There is no established release cadence, and it completely lacks documentation, including a README. Its intended purpose is for advanced TypeScript developers requiring compile-time type validation or metaprogramming, allowing for boolean operations such as AND, OR, and NOT to be performed directly within the type system. This enables the creation of more robust and self-documenting types for intricate conditional logic scenarios, distinguishing itself by offering these utilities as pure type definitions rather than runtime values.
Common errors
-
Cannot find name 'And'.
cause Attempting to import a type-level generic without the 'import type' syntax or referring to it as a runtime value.fixChange `import { And } from 'typescript-logic';` to `import type { And } from 'typescript-logic';`. -
Type 'boolean' is not assignable to type 'True'.
cause Confusing runtime boolean values (`true`, `false`) with their type-level literal counterparts (`True`, `False`).fixEnsure you are using the specific type-level constants (`True`, `False`) provided by the library when constructing generic types, rather than raw `true` or `false` boolean literals.
Warnings
- breaking The package is at version `0.0.0` and was last published over seven years ago. It is highly unstable, experimental, and should not be used in production environments. Significant breaking changes are likely with any future TypeScript version updates, as it relies on internal type system behaviors.
- gotcha This package is entirely type-level. It provides no runtime JavaScript values or functions. Attempting to `require()` or `import` symbols without the `type` keyword will result in runtime errors or incorrect bundling.
- gotcha The package lacks any official documentation (no README), making its usage, API, and specific behaviors difficult to ascertain without direct code inspection.
Install
-
npm install typescript-logic -
yarn add typescript-logic -
pnpm add typescript-logic
Imports
- True
import { True } from 'typescript-logic'; const val: True = true;import type { True } from 'typescript-logic'; - False
import { False } from 'typescript-logic'; const val: False = false;import type { False } from 'typescript-logic'; - And
const And = require('typescript-logic').And;import type { And } from 'typescript-logic'; - Or
import type { Or } from 'typescript-logic'; - Not
import type { Not } from 'typescript-logic';
Quickstart
import type { True, False, And, Or, Not } from 'typescript-logic';
// Define some type-level boolean variables
type IsLoggedIn = True;
type HasAdminPermissions = False;
type IsPremiumUser = True;
// Demonstrate type-level AND logic
type CanAccessAdminPanel = And<IsLoggedIn, HasAdminPermissions>;
// Expected: False
// Demonstrate type-level OR logic
type CanViewPremiumContent = Or<IsPremiumUser, HasAdminPermissions>;
// Expected: True
// Demonstrate type-level NOT logic
type IsGuest = Not<IsLoggedIn>;
// Expected: False
// Combine operations
type CanPerformCriticalAction = And<IsLoggedIn, Or<HasAdminPermissions, IsPremiumUser>>;
// Expected: And<True, Or<False, True>> -> And<True, True> -> True
// Verify the results (compile-time assertion)
type Test1 = CanAccessAdminPanel extends False ? true : false;
const test1Result: Test1 = true;
type Test2 = CanViewPremiumContent extends True ? true : false;
const test2Result: Test2 = true;
type Test3 = IsGuest extends False ? true : false;
const test3Result: Test3 = true;
type Test4 = CanPerformCriticalAction extends True ? true : false;
const test4Result: Test4 = true;