typescript-best-practices

typescript-best-practices is a skill for Claude Code, Codex from monkilabs/opencastle. It costs 63 tokens per session (1,386 once invoked), scanned A, original, MIT.

A TypeScript guide for modelling data so invalid states are harder to represent. It covers unions, branded values, validation, type narrowing, and avoiding unsafe casts.

In plain words
What is it for?
Use it when designing domain types or refactoring TypeScript and TSX code, especially where optional fields, type casts, or external input are involved.
Why use it?
It helps the TypeScript compiler catch more mistakes and reduces runtime failures caused by unchecked external data or incorrect assumptions.

Skill for Claude CodeCodex

Install

Getting it into your agent

One page per mod, every tool's command on it. A separate URL per tool would split the same page into five that compete with each other.

agentmods
npx agentmods add skills/monkilabs/opencastle/typescript-best-practices
Any agent
npx skills add monkilabs/opencastle --skill typescript-best-practices
Clone the repo
git clone --depth 1 https://github.com/monkilabs/opencastle

Made for: Claude Code, Codex.

Wrote this? Show the measurements

A badge with what this costs and how it scanned, read live from this page, so it follows the numbers instead of freezing them. Markdown for a README, HTML for a documentation site or a project page.

agentmods badge for typescript-best-practices

README.md
[![agentmods](https://agentmods.dev/badge/skills/monkilabs/opencastle/typescript-best-practices.svg)](https://agentmods.dev/skills/monkilabs/opencastle/typescript-best-practices)
Your own site
<a href="https://agentmods.dev/skills/monkilabs/opencastle/typescript-best-practices"><img src="https://agentmods.dev/badge/skills/monkilabs/opencastle/typescript-best-practices.svg" alt="Measured on agentmods" height="20"></a>
Per session 63 Skills are progressive disclosure: only the name and description are preloaded; the body loads when the skill is used.
When invoked 1,386 The whole file, excluding the scripts and references it only reads on demand.
Security scan A 0 findings. Scan, not verified.
Origin original No closer match found in the catalogue.
Token cost

What it costs to keep this loaded

Counted locally with the o200k_base tokenizer, which is exact for GPT models; Claude uses its own tokenizer and its counts differ. Treat this as one consistent yardstick across the catalogue rather than a bill. Prices are per million input tokens.

ModelPer sessionOnce invoked
Fable 5 $0.00063 $0.01386
Opus 5 $0.00032 $0.00693
Sonnet 5 $0.00013 $0.00277
Haiku 4.5 $0.00006 $0.00139

Measured yesterday against content hash 753fc87d6749, method: parsed. Prices are Anthropic first-party input rates as of 2026-08-30, from the pricing page.

Security

Grade A, and why

typescript-best-practices scanned grade A with 0 findings against 26 rules in 11 categories — prompt injection, anti-refusal, data exfiltration, privilege escalation, supply chain, agent snooping, system-prompt leakage, SSRF and excessive agency — measured yesterday.

A static scan of the body, not an audit. Every finding is printed with the line that produced it so you can judge whether it matters here. A mod is markdown that instructs an agent; that is exactly why what it instructs is worth reading.

Nothing flagged

None of the 26 patterns this scan looks for appear in this file: no shell pipes, no recursive deletes, no credential paths, no hidden text, no instruction-override or anti-refusal phrasing, no agent-config snooping. That is not a guarantee, it is the absence of the things that are checkable.

src/orchestrator/skills/typescript-best-practices/SKILL.md · 95 lines

How it starts

The opening of the file, as written. The whole thing — 95 lines — stays where its author put it; the contents beside it link to each section on GitHub.

TypeScript Best Practices

Make the illegal state unrepresentable, then let the compiler enforce it.

Rule Summary
Discriminated unions Model variants with a kind literal discriminant so impossible states cannot be represented. No optional-field bags.
Branded types Brand primitives with & { readonly __brand: 'X' } so they cannot be mixed up. Validate once at creation.
Constructive modeling Build the shape so the illegal value cannot be constructed. [T, ...T[]] for non-empty, [T, T][] for even length, start plus duration for a range. Not a runtime guard.
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. any disables type checking everywhere it touches.
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 is safe. Name them isX or hasX.
Exhaustiveness Inline const _exhaustive: never = x in default arms so the compiler errors when a 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> stops at that parse. Trust types inside; do not re-validate deep in call chains.
Schema-derived types Reach for Pick/Omit/Parameters/ReturnType/Awaited/typeof before declaring a new interface.
Object args Pass objects, not positional args, so argument order is self-documenting. Skip on hot paths: per-frame render, tokenizers, parsers.
Real tests Do not mock what you can run. Prefer real test primitives and verify UI in a running build. Mock only what you cannot run locally.
Structured telemetry Structured logger diagnostics with enough context to debug from an id. No console.log in shipped code.

External data is anything from RPC payloads, JSON.parse, postMessage, IPC, file contents, environment variables, or database results.

Read the full file on GitHub · 95 lines

Changes

What this file has done since we first saw it

Hashed on every crawl. A supply-chain change to an agent config is a question of when, not whether, so the history is kept rather than the latest state alone.

  1. yesterday First seen · 95 lines · 63 tokens per session scan A 753fc87d6749

Subscribe to this mod's changes

typescript-best-practices is a skill published in the GitHub repository monkilabs/opencastle (61 stars, last pushed 7d ago), licensed MIT. It adds 63 tokens to every session and 1,386 once invoked, about $0.0003 per session on Opus 5. A static security scan graded it A with 0 findings. No closer match exists in the catalogue, so it is treated as the original; first seen 2026-09-03.