Skip to content

Language TypeScript

Oliver Atkinson edited this page May 31, 2026 · 1 revision

TypeScript

The TypeScript generator emits a stand-alone .ts file.

Generate TypeScript

swift run lexicon-generate commerce.lexicon --type ts -o src/generated/commerce-lexicon

This writes:

src/generated/commerce-lexicon.ts

Import it from application code:

import { commerce } from "./generated/commerce-lexicon";

const submit = commerce.api.order.submit;
console.log(submit.__);

The __ getter returns the exact lemma ID.

Generated Shape

Generated TypeScript defines:

  • I, the shared marker interface.
  • L, the base class that stores the exact ID.
  • one class per concrete lemma.
  • one interface per semantic type.
  • class fields for own children.
  • inherited fields where a type provides children.

Example:

const button = commerce.ui.checkout.button.primary;

sendAnalytics({
  event: commerce.analytics.event.checkout_started.__,
  target: button.__,
});

Synonyms

Synonyms resolve to the generated source member:

const enabled = commerce.ui.product.card.buy.enabled;
const active = commerce.ui.product.card.buy.active;

console.assert(enabled.__ === active.__);

Use this when a UI term and analytics term need to preserve their local vocabulary but represent one concept.

npm Script

{
  "scripts": {
    "generate:lexicon": "swift run lexicon-generate lexicons/commerce.lexicon --type ts -o src/generated/commerce-lexicon",
    "typecheck": "npm run generate:lexicon && tsc --noEmit"
  }
}

If TypeScript builds should not require Swift, generate and commit the .ts file.

Runtime Boundaries

Generated TypeScript gives you stable identifiers and typed navigation. It does not validate runtime JSON payloads by itself.

Pair it with your existing schema tool when crossing network or persistence boundaries:

const key = commerce.api.order.submit.__;
const payload = SubmitOrderSchema.parse(input);

Use Lexicon for vocabulary identity and relationships. Use validation libraries for data shape and runtime type safety.

Clone this wiki locally