typescript-best-practices

Guides TypeScript best practices for type safety, code organization, and maintainability. Use this skill when configuring TypeScript projects, deciding on typing strategies, writing async code, or reviewing TypeScript code quality.

Safety Notice

This listing is imported from skills.sh public index metadata. Review upstream SKILL.md and repository scripts before running.

Copy this and send it to your AI assistant to learn

Install skill "typescript-best-practices" with this command: npx skills add flpbalada/my-opencode-config/flpbalada-my-opencode-config-typescript-best-practices

TypeScript Best Practices

Comprehensive guide to writing clean, type-safe, and maintainable TypeScript code.

When to Use

  • Configuring a new TypeScript project
  • Deciding between interface vs type alias
  • Writing async/await code
  • Reviewing TypeScript code quality
  • Avoiding common TypeScript pitfalls

Quick Reference

// Type inference - let TS do the work
const name = 'Alice';

// Explicit for APIs
function greet(name: string): string { ... }

// Unknown over any
function safe(data: unknown) { ... }

// Type-only imports
import type { User } from './types';

// Const assertions
const tuple = [1, 2] as const;

// Null safety
const len = str?.length ?? 0;

// Guard clauses
if (!valid) throw new Error();
// main logic...

Common Mistakes

MistakeProblemSolution
Overusing anyDefeats type checkingUse unknown, generics, or proper types
Not using strict modeMisses many errorsEnable "strict": true
Redundant annotationsClutters codeTrust type inference
Ignoring union typesRuntime errorsUse type guards
Not handling nullCrashesUse ?. and ?? operators
Nested conditionalsHard to readUse guard clauses
Duplicate types with ZodMaintenance burdenInfer from z.infer<typeof schema>
Sequential awaits for independent opsSlower executionUse Promise.all
Non-Error causeBreaks error chainsAlways use Error instance for cause

Progressive Disclosure

TopicFileWhen to Use
Type system & functionscontext/code-patterns.mdInterface vs type, async patterns, guard clauses
Project structurecontext/organization.mdFile naming, barrel files, configuration
Testing & performancecontext/testing-performance.mdDI, type guards, null handling, performance

Key Principles

  1. Type inference when obvious - Let TypeScript infer simple types
  2. Explicit for public APIs - Document function signatures clearly
  3. Unknown over any - Use unknown with type guards instead of any
  4. Guard clauses - Early returns reduce nesting
  5. Type-only imports - Better tree-shaking with import type

References

Source Transparency

This detail page is rendered from real SKILL.md content. Trust labels are metadata-based hints, not a safety guarantee.

Related Skills

Related by shared tags or category signals.

Coding

trust-psychology

No summary provided by upstream source.

Repository SourceNeeds Review
Coding

social-proof-psychology

No summary provided by upstream source.

Repository SourceNeeds Review
Coding

five-whys

No summary provided by upstream source.

Repository SourceNeeds Review
Coding

cognitive-fluency-psychology

No summary provided by upstream source.

Repository SourceNeeds Review