Tsentials union

From senrecep/tsentials · View interactive version

Tsentials union is an AI agent skill from the GitHub repository senrecep/tsentials. Use when a value can be one of several distinct tagged types — Union<T> as a compile-time-safe discriminated union with Union.match() for exhaustive pattern matching, Union.is() for type guards, and Union.of() for construction.

How to use this skill

Skill file

tsentials/union

Union<T> is a tag-based discriminated union with exhaustive pattern matching. Replaces object-typed returns and unsafe casting.

Installation

npm install tsentials

Import

import { Union } from 'tsentials/union';

Defining a Union

type PaymentResult = Union<{
  success: { transactionId: string };
  pending: { estimatedMs: number };
  failed:  { error: AppError };
}>;

Creating Values

Assign a fresh literal with as, not : — annotating the variable with : PaymentResult narrows it down to whichever member the literal matches, which breaks Union.match exhaustiveness the moment you use that variable:

const result = { tag: 'success', value: { transactionId: 'txn_123' } } as PaymentResult;
const pending = { tag: 'pending', value: { estimatedMs: 3000 } } as PaymentResult;
const failed = { tag: 'failed', value: { error: Err.unexpected('Pay.Failed', 'Payment failed.') } } as PaymentResult;

Union.of(tag, value) also constructs a value, but its type parameter can't be inferred from a Union<T> alias — you must spell out the full record shape T explicitly:

const created = Union.of<{
  success: { transactionId: string };
  pending: { estimatedMs: number };
  failed: { error: AppError };
}, 'success'>('success', { transactionId: 'txn_123' });

The as form above is shorter and equally type-safe — prefer it unless you specifically need Union.of.


Exhaustive Pattern Match

const message = Union.match(result, {
  success: ({ transactionId }) => `Paid! Ref: ${transactionId}`,
  pending: ({ estimatedMs }) => `Pending for ${estimatedMs / 1000}s`,
  failed:  ({ error })       => `Failed: ${error.description}`,
});
// TypeScript errors if any case is missing

Type Guard

if (Union.is(result, 'success')) {
  console.log(result.value.transactionId); // narrowed to { transactionId: string }
}

Extracting Value

// Union.get — throws if tag doesn't match
const { transactionId } = Union.get(result, 'success');

Typical Usage — service return type

type CreateOrderResult = Union<{
  created:    { orderId: string; total: number };
  outOfStock: { productId: string };
  limitExceeded: { limit: number; requested: number };
}>;

function createOrder(request: CreateOrderRequest): CreateOrderResult {
  if (!hasStock(request)) return { tag: 'outOfStock', value: { productId: request.productId } };
  if (exceedsLimit(request)) return { tag: 'limitExceeded', value: { limit: 10, requested: request.qty } };
  return { tag: 'created', value: { orderId: crypto.randomUUID(), total: request.total } };
}

// At API boundary
const response = Union.match(createOrder(request), {
  created:      ({ orderId, total }) => ({ status: 201, body: { orderId, total } }),
  outOfStock:   ({ productId })      => ({ status: 409, body: { error: `${productId} out of stock` } }),
  limitExceeded: ({ limit })          => ({ status: 400, body: { error: `Limit is ${limit}` } }),
});

Union\<T\> vs Result\<T\>

| Use | When |

|-----|------|

| Result<T> | Binary success/failure — one value OR one-or-more errors |

| Union<T> | Multiple distinct outcome types — each branch carries different typed data |


Best Practices

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.