Skip to content

Interactive Playground

typesugar includes an interactive playground that lets you try code directly in your browser without installing anything.

Open the Playground →

What Works in the Playground

The playground runs the full typesugar transformer in your browser. You can:

  • Write and transform — see your code compiled in real time
  • Run the output — execute transformed code with console output
  • Use runtime librariesimport from @typesugar/* packages and run real code
  • Share — copy a URL that encodes your code

Runtime Library Support

The playground bundles runtime implementations of @typesugar/* packages, so imports actually work at runtime — not just at the type level. When you press Run, your code executes with real @typesugar/fp, @typesugar/collections, @typesugar/graph, and more.

Available packages:

PackageWhat You Can Use
typesugar / @typesugar/corestaticAssert, comptime, macro APIs
@typesugar/fpSome, None, Left, Right, pipe, IO
@typesugar/stdExtension methods, pattern matching, ranges
@typesugar/collectionsHashSet, HashMap
@typesugar/graphDiGraph, StateMachine
@typesugar/contractsrequires, ensures, invariant
@typesugar/unitsDimensional analysis
@typesugar/codecSchema codecs
@typesugar/parserParser combinators
@typesugar/symbolicSymbolic expressions, calculus
@typesugar/type-systemNewtype, refined types
@typesugar/typeclass@typeclass, @instance, summon
@typesugar/validateSchema validation
@typesugar/mapperObject mapping

Packages with heavy Node.js dependencies (@typesugar/math, @typesugar/testing, @typesugar/effect) are excluded from the runtime bundle.

Examples

The playground ships with 20+ examples organized by module. Pick one from the Examples dropdown to see a feature in action.

CategoryExamples
Getting StartedWelcome
Core Macros@typeclass, @derive, extension, comptime, pipe & compose, static-assert, reflect, @tailrec
@typesugar/fpOption & Either, Validated, Linked List
@typesugar/stdPattern Matching, Ranges
@typesugar/collectionsHashSet & HashMap
@typesugar/graphDirected Graph, State Machine
@typesugar/contractsDesign by Contract
@typesugar/unitsDimensional Analysis
@typesugar/codecSchema Codec
@typesugar/parserArithmetic Parser
@typesugar/symbolicCalculus

All examples are runnable — they use real runtime libraries, not stubs.

Adding Examples

Examples live in docs/examples/ and are auto-discovered at build time. To add one:

1. Create a file

docs/examples/<module>/<example-name>.ts

The directory name becomes the group in the dropdown. Use an existing directory, or create a new one.

2. Add metadata

Every example starts with //! metadata lines:

typescript
//! Example Title
//! Short description of what this demonstrates

import { Some, None } from "@typesugar/fp";

// Your code here...
console.log(Some(42));

The first //! line is the name shown in the dropdown. The second is the description shown on hover. Everything after the metadata block is the code loaded into the editor.

3. Register the group (optional)

If you created a new directory, add it to the GROUP_META object in docs/.vitepress/components/playground-examples.ts:

typescript
const GROUP_META: Record<string, { label: string; order: number }> = {
  // ...existing groups...
  "my-module": { label: "@typesugar/my-module", order: 55 },
};

Groups without an entry still appear — they just use the directory name as-is and sort to the middle.

Tips for good examples

  • Make it runnable. Use console.log() so pressing Run shows output.
  • Keep it focused. One concept per example. 20–40 lines is ideal.
  • Use real imports. import { ... } from "@typesugar/fp" works in the sandbox.
  • Show the value. Don't just define types — demonstrate behavior.

Embedded Playgrounds

You can embed smaller playgrounds directly in documentation pages:

vue
<PlaygroundEmbed
  code="const x = 42;
console.log(x);"
  height="150px"
/>

Props

PropTypeDefaultDescription
codestringRequiredInitial code content
readonlybooleanfalsePrevent editing
heightstring"300px"Editor panel height
hideOutputbooleanfalseHide the output panel
titlestring""Optional title in header

Every embedded playground has an Open in Playground button that opens the code in the full playground with all settings preserved.

TIP

For code containing angle brackets (like generics), use the full playground or the "Open in Playground" button to expand embedded examples.

Keyboard Shortcuts

ShortcutAction
Cmd/Ctrl + EnterRun code
Cmd/Ctrl + STransform (without running)

Released under the MIT License.