typescript-best-practices

Applies type-system discipline rules when reading or editing TypeScript and TSX files.

Updated Sep 2, 2026
One-click install
npx skills add https://github.com/jnyross/pstack-muse --skill typescript-best-practices-jnyross
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: typescript-best-practices
Source: https://github.com/jnyross/pstack-muse/tree/main/skills/typescript-best-practices
Command: npx skills add https://github.com/jnyross/pstack-muse --skill typescript-best-practices-jnyross

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve? TypeScript codebases often accumulate unsafe patterns like any types, unchecked as casts, and loose object shapes that let invalid states compile and crash at runtime. This Skill gives the AI a concrete rulebook so every .ts or .tsx edit enforces sound typing instead of papering over type errors. ## Core Features & Use Cases - Type-safety rulebook: Sixteen opinionated rules covering discriminated unions, branded types, unknown over any, narrowing hierarchy, exhaustiveness checks, and satisfies over as. - Boundary validation guidance: Enforces parsing external data (RPC payloads, JSON.parse, IPC, environment variables) into named domain types using schema libraries like Zod with z.infer. - Code examples reference: A references/patterns.md file with before/after code for each rule, including constructive modeling with NonEmpty<T> tuples and schema-derived types via Pick/Omit/Parameters. - Use Case: While refactoring a Redux-style state object, the Skill replaces a { loading: boolean; data?: T; error?: string } bag with a discriminated union so impossible states cannot be represented. ## Quick Start Review this TypeScript file and refactor it to follow the typescript-best-practices rules, replacing any as casts and optional-field bags with validated discriminated unions.

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, or parse it with a schema library like Zod.

How to model loading and error states in TypeScript?

Use a discriminated union with a shared literal discriminant field like kind, with variants for loading, ready, and error. This makes contradictory combinations like loading true with data present impossible to represent, unlike boolean-plus-optional-field shapes.

When should I use Zod instead of writing type guards?

Prefer the repository's existing schema library before hand-writing property-by-property guards for external data. Define one schema, derive the type with z.infer, and use safeParse when failure is an expected branch, avoiding drift between schema, interface, and guard.

Why are as casts dangerous in TypeScript?

Every as cast is a potential runtime crash because it overrides the compiler without verifying the claim. Cast only after full validation of the value, and prefer satisfies, which validates the value while preserving literal types instead of widening them.

How do I make a TypeScript switch statement exhaustive?

Add a default arm that assigns the discriminant to a never-typed local, such as const _exhaustive: never = s. The compiler then errors when a new union variant is added without a matching case, catching incomplete handling at compile time.

When should I not use branded types or non-empty arrays?

Keep plain T[] when every operation on it stays total, like summing numbers where empty is valid. Strengthen to NonEmpty<T> or branded primitives only where the loose type forces a non-null assertion, a cast, or a should-never-happen throw at a use site.