Skip to content
OpenAgentSkill
Browsing is open · Catalog captured 2026-11-21 · Automatic updates and account services are paused.
Data & analytics

typescript-types

Apply, review, and explain TypeScript type conventions. Use automatically for tasks involving inference, readonly data, type-safe constants, template literal types, any and unknown, assertions, type errors, type definitions, arrays, type imports, or generated service types.

mkosir · AI review on file

Installation

Read the source and dependencies before running a command. Copying does not install or execute anything.

npx skills add mkosir/typescript-style-guide --skill typescript-types

About this skill


name: typescript-types description: Apply, review, and explain TypeScript type conventions. Use automatically for tasks involving inference, readonly data, type-safe constants, template literal types, any and unknown, assertions, type errors, type definitions, arrays, type imports, or generated service types.

Types

Apply the TypeScript Style Guide's type conventions in the context of the current task.

Workflow

  1. Inspect the consuming repository's conventions and configuration.
  2. Let explicit repository conventions take precedence over this opinionated guidance.
  3. Apply, review, or explain only the guidance relevant to the task.
  4. State important tradeoffs when the appropriate model depends on context or judgment.

Boundaries

  • Keep TypeScript and ESLint responsible for checks they can enforce automatically.
  • Do not introduce unrelated TypeScript Style Guide conventions merely because this skill is active.

Types

When creating types, consider how they would best describe our code.
Being expressive and keeping types as narrow as possible offers several benefits to the codebase:

  • Increased Type Safety - Catch errors at compile time, as narrowed types provide more specific information about the shape and behavior of your data.
  • Improved Code Clarity - Reduces cognitive load by providing clearer boundaries and constraints on your data, making your code easier for other developers to understand.
  • Easier Refactoring - With narrower types, making changes to your code becomes less risky, allowing you to refactor with confidence.

Type Inference

As a rule of thumb, explicitly declare types only when it helps to narrow them.

Explicitly declare types when doing so helps to narrow them:

// ❌ Avoid
const employees = new Map(); // Inferred as wide type 'Map<any, any>'
employees.set('Lea', 17);
type UserRole = 'admin' | 'guest';
const [userRole, setUserRole] = useState('admin'); // Inferred as 'string', not the desired narrowed literal type

// ✅ Use explicit type declarations to narrow the types.
const employees = new Map<string, number>(); // Narrowed to 'Map<string, number>'
employees.set('Gabriel', 32);
type UserRole = 'admin' | 'guest';
const [userRole, setUserRole] = useState<UserRole>('admin'); // Explicit type 'UserRole'

Avoid explicitly declaring types when they can be inferred:

// ❌ Avoid
const userRole: string = 'admin'; // Inferred as wide type 'string'
const employees = new Map<string, number>([['Gabriel', 32]]); // Redundant type declaration
const [isActive, setIsActive] = useState<boolean>(false); // Redundant, inferred as 'boolean'

// ✅ Use type inference.
const USER_ROLE = 'admin'; // Inferred as narrowed string literal type 'admin'
const employees = new Map([['Gabriel', 32]]); // Inferred as 'Map<string, number>'
const [isActive, setIsActive] = useState(false); // Inferred as 'boolean'

Data Immutability

Immutability should be a key principle. Wherever possible, prevent unintended mutations with types like Readonly and ReadonlyArray.

  • Readonly types help prevent accidental mutations and bugs caused by unintended side effects. This protection exists during type checking and does not freeze values at runtime.
  • When performing data processing, always return new arrays, objects, or other reference-based data structures. To minimize cognitive load for future developers, strive to keep data objects flat and concise.
  • Use mutations sparingly, only in cases where they are truly necessary, such as when dealing with complex objects or optimizing for performance.
// ❌ Avoid data mutations
const removeFirstUser = (users: Array<User>) => {
  if (users.length === 0) {
    return users;
  }
  return users.splice(1);
};

// ✅ Use readonly type to prevent accidental mutations
const removeFirstUser = (users: ReadonlyArray<User>) => {
  if (users.length === 0) {
    return users;
  }
  return users.slice(1);
  // Using arr.splice(1) errors - Function 'splice' does not exist on 'users'
};

Type-Safe Constants With Satisfies

The as const satisfies syntax combines narrow, readonly inference with type validation. It is useful when a constant should retain its exact values while conforming to a broader type.

Key benefits:

  • Readonly values with as const
    • Ensures the constant is treated as readonly.
    • Narrows the types of values to their literals, preventing accidental modifications.
  • Validation with satisfies
    • Ensures the object conforms to a broader type without widening its inferred type.
    • Helps catch type mismatches at compile time while preserving narrowed inferred types.

Array constants:

type UserRole = 'admin' | 'editor' | 'moderator' | 'viewer' | 'guest';

// ❌ Avoid constant of wide type
const DASHBOARD_ACCESS_ROLES: ReadonlyArray<UserRole> = ['admin', 'editor', 'moderator'];

// ❌ Avoid constant with incorrect values
const DASHBOARD_ACCESS_ROLES = ['admin', 'contributor', 'analyst'] as const;

// ✅ Use readonly constant of narrowed type
const DASHBOARD_ACCESS_ROLES = ['admin', 'editor', 'moderator'] as const satisfies ReadonlyArray<UserRole>;

Object constants:

type OrderStatus = {
  pending: 'pending' | 'idle';
  fulfilled: boolean;
  error: string;
};

// ❌ Avoid mutable constant of wide type
const IDLE_ORDER: OrderStatus = {
  pending: 'idle',
  fulfilled: true,
  error: 'Shipping Error',
};

// ❌ Avoid constant with incorrect values
const IDLE_ORDER = {
  pending: 'done',
  fulfilled: 'partially',
  error: 116,
} as const;

// ✅ Use readonly constant of narrowed type
const IDLE_ORDER = {
  pending: 'idle',
  fulfilled: true,
  error: 'Shipping Error',
} as const satisfies OrderStatus;

Template Literal Types

Embrace template literal

Read the complete source ↗