typescript-best-practices

Applies type-safety rules and patterns when reading or editing TypeScript files.

136|8|Updated May 9, 2026
One-click install
npx skills add https://github.com/Sma1lboy/rove --skill typescript-best-practices-sma1lboy
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: typescript-best-practices
Source: https://github.com/Sma1lboy/rove/tree/main/.agents/skills/pstack/skills/typescript-best-practices
Command: npx skills add https://github.com/Sma1lboy/rove --skill typescript-best-practices-sma1lboy

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve? TypeScript codebases often accumulate unsafe patterns like any, unchecked as casts, and loose types that let invalid states compile, leading to runtime crashes that the compiler should have caught. ## Core Features & Use Cases - Type Modeling Rules: Enforces discriminated unions, branded types, and constructive modeling so impossible states cannot be represented in .ts and .tsx files. - Safe Narrowing Guidance: Provides a narrowing hierarchy from discriminant switches down to validated as casts, plus exhaustiveness checks with never assignments. - Boundary Validation: Directs parsing of external data as unknown at system boundaries into named domain types, with schema-derived types via Pick, Omit, and Parameters. - Use Case: When refactoring a module that parses JSON-RPC payloads, apply this Skill to replace any and as casts with validated parse functions and discriminated union state types. ## Quick Start Review this TypeScript file and refactor it to follow the typescript-best-practices rules, replacing any unsafe casts with validated narrowing.

Frequently Asked Questions about typescript-best-practices

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I replace any with unknown in TypeScript?▼

Type external data as unknown instead of any, since any disables type checking everywhere it touches. Narrow unknown with typeof, instanceof, or the in operator before accessing properties, so the compiler verifies each access.

How to model states with discriminated unions in TypeScript?▼

Give every variant a shared literal discriminant field like kind with a unique value per variant, for example { kind: "loading" } or { kind: "ready"; diff: GitDiff }. This makes contradictory states unrepresentable, unlike boolean flags plus optional fields.

When should I use satisfies instead of as in TypeScript?▼

Use satisfies when you want to validate a value against a type while preserving its literal types, such as { theme: "dark" } satisfies Config. Use as only after runtime validation has verified the claim, since every as is a potential runtime crash.

Does TypeScript support non-empty array types?▼

Yes, model a non-empty array as a variadic tuple: type NonEmpty<T> = [T, ...T[]]. Narrow a plain array once with a guard like arr.length > 0, and the non-empty fact then travels in the type without repeated checks.

Why does my switch statement miss new union variants?▼

Without an exhaustiveness check, adding a variant to a union does not error in existing switches. Add const _exhaustive: never = x; in the default arm so the compiler errors when a new variant is unhandled.

When should I avoid object arguments in TypeScript functions?▼

Object arguments are preferred for self-documenting, order-independent calls, but skip them on hot paths. Per-frame rendering, tokenizers, and parsers in tight loops should avoid the allocation cost of creating argument objects.