Skip to content

feat(web): generate literal-union TS types for token paths#27

Open
Gr1dlock wants to merge 2 commits into
mainfrom
feat/ts-token-types
Open

feat(web): generate literal-union TS types for token paths#27
Gr1dlock wants to merge 2 commits into
mainfrom
feat/ts-token-types

Conversation

@Gr1dlock
Copy link
Copy Markdown
Contributor

@Gr1dlock Gr1dlock commented Jun 2, 2026

What

The published web package shipped index.d.ts as declare const tokens: Record<string, string>, so consumers (e.g. the app-backend SDUI V4 library, which references tokens by their canonical path) got no compile-time safety — any string passed.

This generates index.d.ts from the token definitions, emitting literal-union types:

  • ColorToken — every canonical color path: semantic.color.text.primary, semantic.color.border.translucent, primitive.color.grey.950, …
  • TypographyToken — grouped typography.{category}.{id} paths (typography.headline.h1, typography.body.b2, …). The category (display/numeric/headline/subtitle/label/body/caption) is derived from the font-token id prefix, matching the UI Kit 5 grouping.

The default tokens: Record<string, string> export is preserved for backward compatibility.

Changes

  • tokens/formats/types-web.ts — new generator (path → literal-union types)
  • tokens/build/types.tsbuildWebTypes() loads color + font definitions, writes build/web/index.d.ts
  • tokens/build/index.ts — runs buildWebTypes() in the build
  • tokens/build/web-package.ts — stop copying the static index.d.ts (now generated)
  • removed the static tokens/templates/web/index.d.ts

Notes

  • index.d.ts lands in build/web (the published artifact), same as the other web outputs — no committed generated file.
  • Consumers can now import type { ColorToken, TypographyToken } from '@worldcoin/nucleus' and satisfies their token maps against them.
  • npm run build, typecheck, lint, format:check all pass.

Part of adopting Nucleus tokens in app-backend SDUI V4.

🤖 Generated with Claude Code

The published web package shipped `index.d.ts` as
`declare const tokens: Record<string, string>`, so consumers got no
compile-time safety when referencing token paths.

Generate `index.d.ts` from the token definitions instead, emitting:
- `ColorToken` — every canonical color path (`semantic.color.text.primary`,
  `primitive.color.grey.950`, …)
- `TypographyToken` — grouped `typography.{category}.{id}` paths, with the
  category derived from the font-token id prefix (UI Kit 5 grouping)

The default `tokens: Record<string, string>` export is kept for back-compat.
`index.d.ts` is now produced by the build (build/types.ts + formats/types-web.ts)
rather than copied from a static template.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
UI Kit 4.0 names the category 'Number' and has no 'caption'. Emit
typography.number.* (not numeric) and drop the unused caption mapping.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants