Community程式設計與開發github.com

typescript-ui

Use whenever writing UI code in TypeScript — .ts/.tsx components, hooks, props, state, forms, API data in React/Next/Vue/Svelte/Solid. Modern TypeScript best practices that make the new-ui and responsive-ui rules enforceable by the type checker. Called by /new-ui; also invoked manually as /typescript-ui.

typescript-ui 是什麼?

typescript-ui is a Claude Code agent skill that use whenever writing UI code in TypeScript — .ts/.tsx components, hooks, props, state, forms, API data in React/Next/Vue/Svelte/Solid. Modern TypeScript best practices that make the new-ui and responsive-ui rules enforceable by the type checker. Called by /new-ui; also invoked manually as /typescript-ui.

相容平台~Claude Code~Codex CLI~Cursor
Part ofReact
npx skills add https://github.com/Cucuteanu-Tudor-912/UI-DESIGN-SKILLS/tree/HEAD/skills/typescript-ui

Installed? Explore more 程式設計與開發 skills: steipete/bluebubbles, steipete/eightctl, steipete/blucli · View all 6 →

在你喜歡的 AI 中提問

開啟一個已預先載入此 Agent Skill 的新對話。

說明文件

TypeScript for UI

Make the type checker enforce the UI rules, so a missing state, alt text or label becomes a compile error instead of a bug. Follow the repo's existing tsconfig, lint rules and patterns first. The rules below fill the gaps.

1. Compiler baseline

  • Turn on strict: true. If the repo allows it, also enable noUncheckedIndexedAccess, exactOptionalPropertyTypes and noImplicitOverride. Don't loosen the existing config to make code compile.
  • Use modern module settings:
    • "module": "preserve" or "esnext" with "moduleResolution": "bundler" for apps built with a bundler;
    • "verbatimModuleSyntax": true;
    • import type for type-only imports.
  • Typecheck before you're done. Run tsc --noEmit (or the repo's typecheck script) and finish only when it reports zero errors.

2. No escape hatches

  • No any. Use unknown and narrow it.
  • No @ts-ignore. If an escape is truly needed, use @ts-expect-error with a one-line reason.
  • No non-null ! and no as casts to silence errors. Fix the type instead.
    • Exception: as const.
    • Use satisfies to check a value against a type without widening it: const routes = {…} satisfies Record<string, Route>.
  • Validate data at trust boundaries. API responses, localStorage, URL params, form data and postMessage are unknown until a schema parses them.
    • Use the repo's validator (Zod, Valibot, ArkType…) and infer types from the schema: type User = z.infer<typeof User>.
    • Never cast await res.json() as User.

3. Model UI states so impossible ones can't exist

This rule enforces "loading, empty, error, success" from new-ui.

  • Use discriminated unions, not boolean soup:
    type State<T> =
      | { status: "loading" }
      | { status: "error"; error: Error }
      | { status: "success"; data: T };
    
    Avoid isLoading + error? + data? together, which allows contradictory combinations.
  • Check every union exhaustively. Use switch with a never default so adding a state forces every view to handle it:
    default: { const _exhaustive: never = state; return _exhaustive; }
    
  • Treat empty as a real state. Use data.length === 0 and render it.
  • Model variants with unions. Use literal unions ("primary" | "secondary") or as const objects instead of enum.

4. Component props

  • Type props explicitly. Use type Props = {…}, or interface if the repo prefers it.
  • Don't use React.FC. Type the props parameter: function Button({ … }: Props).
  • Extend native elements instead of re-declaring their attributes.
    • Write type Props = ComponentPropsWithoutRef<"button"> & { variant?: Variant } and spread the rest onto the element.
    • This passes through aria-*, type, disabled and event handlers for free.
  • Make accessibility required in the type:
    • Image components: require alt: string, plus width and height (or aspectRatio), so the image doesn't shift the layout.
    • Icon-only buttons: require an accessible name, e.g. { icon: Icon; label: string }, or a union that demands aria-label when there are no children.
    • Form fields: require label.
  • Make mutually exclusive props a union, not optional fields. For example: { href: string } | { onClick: () => void }.
  • Children and polymorphism:
    • Use ReactNode for children.
    • Use an as/asChild prop only if the repo's component library already uses that pattern.
  • Use readonly for prop arrays and objects you don't mutate.

5. Events, refs, hooks

  • Use the platform event types: ChangeEvent<HTMLInputElement>, FormEvent<HTMLFormElement>, KeyboardEvent. Read e.currentTarget, not e.target cast.
  • Type refs with the element: useRef<HTMLDivElement>(null). Forward refs the way the repo's React version does: ref as a prop in React 19, forwardRef before that.
  • Let inference work.
    • Annotate only where inference fails, e.g. useState<User | null>(null).
    • Leave out return type annotations on components.
    • Give exported utility functions explicit return types.
  • Custom hooks return objects, or as const tuples.

6. Responsive and theming in types

  • Name breakpoints, tokens and variants from a single as const source and derive the types from it:
    const breakpoints = { sm: "40rem", md: "64rem" } as const;
    type Breakpoint = keyof typeof breakpoints;
    
    Don't use magic strings scattered through components.
  • Use template literal types for constrained strings, e.g. `${number}rem` and `--${string}` for CSS vars.
  • Type CSS custom properties through CSSProperties & Record<--${string}, string>. Don't cast style objects.

7. Async and data

  • Use the repo's data layer (TanStack Query, SWR, RSC, loaders…) and its typed results. Don't hand-roll fetch state in useEffect.
  • Handle every Promise. Nothing floats. Use await, void with a comment, or a .catch.
  • Type errors as unknown in catch and narrow with instanceof Error.

Checklist before done

  • tsc --noEmit passes with zero errors and lint is clean.
  • No new any, as casts, ! assertions or @ts-ignore.
  • External data is parsed by a schema, not cast.
  • UI states are a discriminated union, and every switch is exhaustive with never.
  • Accessibility props (alt, label, accessible name) and media dimensions are required by the types.
  • Props extend native element props, and the rest is spread through.
  • Tokens, breakpoints and variants come from a single as const source.

Individual skills in this repo

This repo contains 2 individual skills — each has its own dedicated page.

相關技能