AGENT SKILL

typescript-best-practices

Sixteen type-safety rules for writing and reviewing TypeScript, with do and don't examples in references/patterns.md. Use whenever you open or edit a .ts or .tsx file, or on /typescript-best-practices.

License 许可
Apache-2.0
最近更新
2026-11-13
Stars
★ 1.6k
Forks
483

01 前言 Front Matter

name
typescript-best-practices
description
Sixteen type-safety rules for writing and reviewing TypeScript, with do and don't examples in references/patterns.md. Use whenever you open or edit a .ts or .tsx file, or on /typescript-best-practices.
disable-model-invocation
true

02 资源文件 1

03 SKILL.md 正文

typescript-best-practices

Read principle-type-system-discipline (../principle-type-system-discipline/SKILL.md) first. These rules apply it to TypeScript. references/patterns.md has a short code example for each rule, numbered to match.

If your agent supports path-scoped skills, attach this one to **/*.ts and **/*.tsx. Otherwise the router or the user loads it when TypeScript is in play. For other languages, use principle-type-system-discipline and principle-boundary-discipline (../principle-boundary-discipline/SKILL.md) directly.

Before you write anything, find the repo's existing conventions: its schema library, its logger, the name it uses for union tags, and the shape of its brand marker. Follow them over the examples in the reference.

Modeling

  1. Tagged unions for variants. Model alternative states as a union of object types that share one literal tag field. A boolean flag plus optional fields lets contradictory combinations exist (loading: true with an error set). Use one tag name across the codebase.
  2. Branded primitives. Intersect a primitive with a readonly brand property so an InvoiceId cannot be passed where a CustomerId is expected. Mint the brand only through a type guard that validates (value is InvoiceId) and a parse function that wraps the guard and throws on failure. Never brand with as. Check once where the value enters; downstream code trusts the brand.
  3. Make illegal values unconstructible. Prefer a shape that cannot hold a bad value to a runtime check: [T, ...T[]] for a non-empty list, Array<[K, V]> for paired data, a start plus a duration instead of start and end with an ordering invariant in a comment. Provide helpers for derived views (the computed end time). Brand the duration only if some unrelated number could be passed in its place.
  4. Use the simplest type that keeps every operation total. A plain array is fine while every operation on it works for empty input (a sum of nothing is zero). Strengthen to non-empty only where the weak type forces a lie: a !, an index cast, or an "unreachable" throw. Or widen the return type to include undefined. Provide a guard so a plain array can be checked once and then carried as non-empty.

Untrusted data

  1. External data is unknown, never any. RPC payloads, parsed JSON, postMessage events, IPC, files, environment variables, and database rows all arrive as unknown and get narrowed before use. any turns off the checker exactly where an attacker controls the input.
  2. Schemas before hand-written guards. Check whether the codebase uses a runtime validation library and already defines a schema for this data. Define one schema and infer the static type from it. Use the non-throwing parse call when bad input is an expected outcome (a user form), and the throwing one when it is a bug. Never add a validation library for one guard. If the static type already exists, annotate the schema with it so the compiler rejects a schema that checks less than the type promises.
  3. No as except after validation. When you remove an assertion, find out why inference failed: add a missing discriminant, narrow a source typed as Record<string, unknown>, parse at the untyped boundary with the owning schema, or, if the fact truly cannot be expressed, use a brand or satisfies.
  4. Narrow in this order, best first: switch on the tag; an in check; typeof or instanceof; a custom guard; an assertion after validation.
  5. Guards must check what they claim. A guard that lies is worse than as because its name promises safety. Name guards isX or hasX. Prefer tag narrowing when a tag exists.
  6. Exhaustive switches. In the default branch of a switch over a union, assign the value to a local typed never. Adding a variant then fails to compile at every switch that forgot it. Return that local in value-returning switches; mark it used with void in statement switches.
  7. satisfies over as for literals. It checks conformance and keeps the literal types.
  8. Parse once at the boundary. Data crossing into the system is parsed once into a named domain type. A loose record type never travels past that parse. Wire formats tolerate unknown fields so older code can read newer messages. Persisted JSON is a versioned blob parsed inside try/catch. Inner functions do not re-validate. See principle-boundary-discipline.
  9. Derive types from generated sources. When types already come from protobuf, OpenAPI, GraphQL, or database migrations, build on them with Pick, Omit, Partial, and typeof. A hand-written duplicate interface drifts the first time the source changes.

Calls, tests, and telemetry

  1. One object parameter over several positional ones. Swapped positional arguments of the same type compile without complaint; named fields do not. Exempt hot paths (per-frame rendering, tokenizers, parsers, tight loops) where the allocation shows up in a profile.
  2. Run real code in tests. Do not mock what can run on a dev machine. Use the test framework's own facilities, including its checks for leaked handles and undisposed resources. Check UI behavior in a real running build. Keep mocks for dependencies that cannot run locally. See the tdd skill (../tdd/SKILL.md).
  3. Structured telemetry. Log through the project's structured logger with enough fields that an id alone lets someone debug the case. No bare console.log in shipped code. Never log secrets, tokens, or full request bodies that may contain them.

Done when

No any touches external data, every as follows a validation, no guard lies, every switch over a union is exhaustive, and boundary data is parsed once into a domain type.

04 同仓库更多 Skills

hashgraph-online
hashgraph-online/awesome-codex-plugins

migrate-to-e2e

Moves an existing Playwright, Cypress, Selenium, Appium, Detox, Maestro, or TesterArmy suite to e2e one slice at a time, with the old suite running beside the new one until each ported test is proven. Keeps a MIGRATION.md progress file so any later session resumes where the last stopped. Optionally plants a defect to show both suites catch it. Use for /migrate-to-e2e, "migrate our tests to e2e", "port these Maestro flows", "move our TesterArmy tests", or when a project has an e2e config and an older suite still running.

★ 1.6k
hashgraph-online
hashgraph-online/awesome-codex-plugins

e2e

Writes, runs, and debugs end-to-end tests for web and mobile apps with e2e, the agentic test runner, which mixes agent.act goals with exact locator assertions on a Playwright browser or an iOS or Android device. Also drives a project's running app live over the e2e MCP server to look at a screen, check a UI change, take a screenshot or record a video, and try a locator; and runs bug bashes, parallel e2e explore runs with each bug proven by a repro test. Use when a project depends on e2e or has an e2e.config.ts, when asked for end-to-end, browser, mobile, or agentic UI tests, to check a change in the running app, to QA or bug bash an app or branch, or when an e2e run fails.

★ 1.6k
hashgraph-online
hashgraph-online/awesome-codex-plugins

create-verification-skill

Generate a project-local verify-<app> skill on top of e2e, so any coding agent can launch the app, check it is worth driving, exercise a feature the way a user does, keep evidence, and bug bash it. Writes the skill, a feature map with one explore charter per feature, and proves it once. Use for /create-verification-skill, "make a verification skill for this repo", or when a project has no scripted way to prove UI behavior.

★ 1.6k
hashgraph-online
hashgraph-online/awesome-codex-plugins

deja-search

deja-vu memory — search the user's past AI coding sessions with the deja CLI. Holds what they already settled and never wrote into the repo (naming rules, banned or already-tried tools, required checks before merge, preferred values, who owns what). Use before your first edit in a task, before you call a change done or ready to merge, before you recommend a tool, library, design or reviewer, before re-debugging an error that may already be solved, when they say 'didn't we fix this before', 'what did we decide about X' or 'I already have that', and before telling them something on this machine does not exist.

★ 1.6k
hashgraph-online
hashgraph-online/awesome-codex-plugins

deja-reflect

End-of-session reflection and memory curation with deja-vu. Review what was decided, what commands broke and what repaired them, and record durable project rules or decisions with deja remember so the next session starts with them.

★ 1.6k
hashgraph-online
hashgraph-online/awesome-codex-plugins

revid

Make, edit, export and post AI videos with Revid (revid.ai) through the Revid MCP server. Use whenever the user wants a short-form or long-form video made (from an idea, a script, a URL, an article, a PDF, a tweet, a product page, their own clips, or a song), wants to find what is going viral on TikTok, YouTube, Instagram or X before making something, wants to edit or re-export an existing Revid project, generate a standalone AI image or clip, make a series episode, or publish and schedule a finished video to connected social accounts. Covers setup (hosted MCP with OAuth, or an API key), the 36 tools, credits, polling and the rules for publishing. Routes to the more specific revid-* skills when one fits.

图像与多媒体Skill与智能体开发
★ 1.6k