Skip to content
Skillpedia
General coding15k · #11002

typescript-best-practices

What it does

The skill gives rules for writing reliable TypeScript code. It is for developers who want fewer type errors and fewer hidden crashes.

When to use it

  • ✓When you ask the agent to write or edit TypeScript or TSX files.
  • ✓When you want unsafe type casts or loose types removed from your code.
  • ✓When you want outside data checked before the code relies on it.

Skill link

https://github.com/cursor/plugins/blob/main/harness/pstack/claude-code/skills/typescript-best-practices/SKILL.md

Copy it and paste it into your AI (Claude, ChatGPT or Cursor), asking it to follow the instructions.

How to use

The easiest way: copy the message and paste it into your AI. It reads the skill and follows the instructions.

Ready-to-paste message

Use this skill to help me: https://github.com/cursor/plugins/blob/main/harness/pstack/claude-code/skills/typescript-best-practices/SKILL.md Read the SKILL.md file at that link and follow its instructions for my task.
Want your AI to find skills like this on its own?Install the free Skillpedia MCP: one line, and it searches 15,000+ skills before each task.Install in 1 minute →

Example of what to ask your AI

“Clean up the types in this TypeScript file so it does not rely on unsafe casts.”

5 new skills a week. 1 minute to read.

How it works

The agent follows a set of rules, such as using labeled variants, using unknown for outside data, and checking data with a schema before trusting its type. The rules keep impossible states out of the code.

Technical content (original SKILL.md)

Original text by the author, straight from GitHub.

TypeScript best practices

Apply the type-system-discipline principle skill first.

Rule Summary
Discriminated unions Model variants with a kind literal discriminant so impossible states can't be represented. No optional-field bags.
Branded types Brand primitives with & { readonly __brand: "X" } so they can't be mixed up. Validate once at the boundary. A brand comes from a verifying type guard (input is AgentId), never from as.
Constructive modeling Build the shape so the illegal value can't be constructed. [T, ...T[]] for non-empty, [T, T][] for even length, start plus duration for a range. Not a runtime guard, not a wish for refinement types.
Simplest total type Keep T[] while every operation on it stays total. Strengthen to NonEmpty<T> only where the loose type forces !, a cast, or a "should never happen" throw.
unknown over any External data is unknown.
Schemas before guards Before hand-writing a property-by-property type guard, use the repository's runtime schema library and infer the type from the schema, such as z.infer.
No as casts Every as is a runtime crash waiting. Cast only after validation.
Narrowing hierarchy Discriminant switch > in operator > typeof/instanceof > user-defined type guard > as.
Type guards Must verify the claim. A lying guard is worse than as because the bug hides behind a name that says it's safe. Name them isX or hasX.
Exhaustiveness Inline const _exhaustive: never = x; in default arms so the compiler errors when a new variant is added.
satisfies over as Validates the value without widening literal types.
Boundary validation Parse where data crosses in, into a named domain type. Record<string, unknown> (however spelled) stops at that parse. Trust types inside. See the boundary-discipline principle skill.
Schema-derived types Reach for Pick/Omit/Parameters/ReturnType/Awaited/typeof before declaring a new interface.
Object args Pass objects, not positional, so argument order is self-documenting. Skip on hot paths (per-frame render, tokenizers, parsers).
Real tests Don't mock what you can run. Prefer the framework's real test primitives with leak/disposable checks, and verify UI in a running build. Mock only what you can't run locally.
Structured telemetry Prefer structured logger diagnostics with enough context to debug from an id. No console.log in shipped code.

Examples: references/patterns.md.

FAQ

Does it work for plain JavaScript files?

The rules are for TypeScript files, meaning .ts and .tsx.

What is a branded type?

A type that marks a value, such as an ID, so it cannot be mixed up with a different value.

Why avoid as casts?

A cast tells the compiler to trust a value without checking it, so the code can fail at runtime.

Details

Original description by the author

TypeScript best practices. Use when reading or editing any .ts or .tsx file.

repo: cursor/plugins

Popularity

GitHub stars
15k
Ranking position
#11002
License
—
Updated
10/31/2026

Is this skill useful?

0

Sign in to vote.

Comments

  • …

More skills from this repository

Similar skills