arktype_skill

This skill helps you design robust discriminated unions in TypeScript using arktype's merge and or patterns for scalable commands and events.
  • TypeScript

4.2k

GitHub Stars

1

Bundled Files

2 months ago

Catalog Refreshed

3 months ago

First Indexed

Readme & install

Copy the install command, review bundled files from the catalogue, and read any extended description pulled from the listing source.

Installation

Preview and clipboard use veilstrat where the catalogue uses aiagentskills.

npx veilstrat add skill epicenterhq/epicenter --skill arktype

  • SKILL.md9.6 KB

Overview

This skill documents practical patterns for building discriminated unions with Arktype using .merge(), .or(), spread key syntax, and type composition. It focuses on composing a reusable base Type with per-variant fields, clear discriminants, and JSON-schema-friendly optionals. Use these patterns when defining commands, events, or any variant-rich schemas in TypeScript.

How this skill works

Create a reusable base Type and extend it per variant via .merge() or the '...' spread key, then combine variants into a union with .or() or the static type.or() form. Arktype auto-detects a discriminant when each branch sets the same key to distinct literal values. Respect .merge() limitations: it only accepts object Types, so merge each variant individually and then union them.

When to use it

  • Defining command, event, or action schemas with distinct variants
  • Composing a shared base Type with variant-specific fields
  • When you need runtime-inspectable and composable Types
  • When you want TypeScript-safe narrowing via a literal discriminant
  • Building schemas that will convert reliably to JSON Schema

Best practices

  • Prefer commandBase.merge({...}) for per-variant fields so the base remains a true arktype Type
  • For 5+ variants prefer type.or(variantA, variantB, ...) to avoid deeply nested chaining
  • For 2–4 variants chaining .or() on merged branches is readable and fine
  • Use 'key?' syntax for optional properties; avoid | undefined on required keys
  • Remember .merge() is shallow: top-level keys are overwritten by merge arguments

Example use cases

  • Command schemas for a background worker where each action has its own payload and optional result
  • Event streams where base metadata (id, createdAt, deviceId) is shared across event types
  • Define table schemas with union value types for use with defineTable()
  • API request/response shapes where each route uses a discriminant literal
  • Local-first app message passing between UI and worker using explicit variants

FAQ

type.or() is recommended for 5+ variants because it avoids deeply nested .or() chains and reads as a flat list of branches.

Can I pass a union into .merge()?

No. .merge() only accepts object Types. Merge each variant separately, then combine them with type.or().

How do I represent an optional value that can be explicitly undefined?

Use the 'key?' optional-key syntax and, if the value can be undefined when present, combine with .or('undefined') for the value type.

Built by
VeilStrat
AI signals for GTM teams
© 2026 VeilStrat. All rights reserved.All systems operational
arktype skill by epicenterhq/epicenter | VeilStrat