marius-townhouse

mirror-types

Use when depending on external types. Use when avoiding tight coupling. Use when external types might change. Use when building adapters. Use when types are only used internally.

marius-townhouse 4 1 Updated 7mo ago
GitHub

Install

npx skillscat add marius-townhouse/effective-typescript-skills/mirror-types

Install via the SkillsCat registry.

About this skill

We need to produce a 2-3 sentence plain-text summary, objective, factual, no marketing language, no superlatives, no calls to action. Must be natural prose, no bullet points, no headings, no markdown. At most 60 words. Must not wrap answer in quotes. Only the summary text. We need to explain what the skill does, what problem it solves, when to use it. 2-3 sentences, plain text, no formatting. Let's craft: "The skill involves creating local copies of external types to isolate your code from changes in those types.

SKILL.md

Mirror Types to Sever Dependencies

Overview

When you depend on types from external packages, changes to those types can break your code. Mirroring types - creating your own local copies of external types - severs this dependency. This is useful when you only need a subset of external types or when you want to insulate yourself from external changes.

When to Use This Skill

  • Depending on external types
  • Avoiding tight coupling to external packages
  • External types might change frequently
  • Building adapters or wrappers
  • Types are only used internally

The Iron Rule

Mirror external types when you want to sever dependencies. Define only the subset you need, insulated from external changes.

Example

// BAD: Direct dependency on external type
import { ExternalUser } from 'external-library';

interface MyService {
  processUser(user: ExternalUser): void;
}

// If ExternalUser changes, your code breaks

// GOOD: Mirror the type
interface User {
  id: string;
  name: string;
  email: string;
}

interface MyService {
  processUser(user: User): void;
}

// Adapter converts external to internal type
function adaptExternalUser(external: ExternalUser): User {
  return {
    id: external.id,
    name: external.name,
    email: external.email,
  };
}

Reference

  • Effective TypeScript, 2nd Edition by Dan Vanderkam
  • Item 70: Mirror Types to Sever Dependencies

Categories