monkilabs/opencastle/typescript-best-practices
Claude Code and agent skill by monkilabs/opencastle
- community
Checking that the SKILL.md file is still in the repository.
Type design in TypeScript: discriminated unions, branded types, narrowing, exhaustiveness and boundary validation. Use when modelling domain types, tightening strictness or generics, removing `as` casts, or migrating code to stricter types.
Install
npx vskill@latest install monkilabs/opencastle/typescript-best-practicesSKILL.md
The same SKILL.md was published earlier as monkilabs/opencastle/typescript-best-practices.
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.
Patterns
Discriminated union. If a bug forces the question "can this combination actually happen?", the type is too loose.
Branded type. Validate once at creation; downstream code trusts the type.
Constructive modeling. Build from parts that are all legal instead of restricting a loose type with runtime checks. TypeScript has no refinement types, and you do not need one.
Simplest total type. Do not strengthen everything. const sum = (xs: number[]) => xs.reduce((a, b) => a + b, 0) is fine, because [] is 0. Strengthen when the loose type forces a lie at a use site; the tells are !, arr[0] as T, and a "should never happen" throw.
Weakening the result to Session | undefined is the other total signature. Either way the empty case lands at the call site, the one place that knows what empty means.
Earned cast. When refactoring an as out of existing code, identify why the compiler cannot infer: a missing discriminant (add one), an overly wide source type (narrow it), an untyped boundary (add a parse function or schema), or something genuinely inexpressible (use a brand or satisfies).
Exhaustiveness. Return-style in value-returning switches, void-style in statement switches.
`satisfies` over `as`. const config = { theme: 'dark', cols: 3 } satisfies Config validates and keeps config.theme as the literal 'dark'. The as Config form widens it to string.
Adapted from the typescript-best-practices skill in cursor/plugins, MIT, copyright 2026 Lauren Tan.
Security
- Security scanPassed, 93 of 100Pattern checks on the skill's files.
- Model reviewNot runA model review is scheduled for the next rescan.
Quality evaluation
- Task evaluationNot runNo evaluation yet. Run one in Skill Studio.
Source
- SKILL.md in the repositoryCheckingsrc/orchestrator/skills/typescript-best-practices/SKILL.md
Popularity
- Repository stars
- 62
- Repository forks
- 12
- 7-day trend score
- 21
Stars and forks are for the whole GitHub repository, not this skill alone. Not refreshed yet.
Works with
Any agent vskill installs to: 56 targets in all. These 39 have a page on this site.
- Claude Code
- Cursor
- GitHub Copilot
- Windsurf
- Codex
- Gemini CLI
- Cline
- Amp
- Roo Code
- Goose
- Continue
- Kilo Code
- Junie
- Trae
- OpenHands
- Qwen Code
- AI Agent
- Tabnine Chat
- Sourcegraph Cody
- Amazon Q
- Firebase Genkit
- Sweep
- Devin
- Phind
- Mentat
- Claude Desktop
- Aider
- GPT Pilot
- Plandex
- bolt.new
- v0
- Replit Agent
- Anse
- TypingMind
- BetterChatGPT
- LibreChat
- Warp AI
- Pieces
- Fig AI
Embed badge
[](https://verified-skill.com/skills/monkilabs/opencastle/typescript-best-practices)