immutability_skill

This skill enforces immutable patterns by returning new objects for updates, preventing bugs and improving React state predictability.
  • TypeScript

6

GitHub Stars

1

Bundled Files

2 months ago

Catalog Refreshed

4 months ago

First Indexed

Readme & install

Copy the install command, review bundled files from the catalogue, and read any extended description pulled from the listing source.

Installation

Preview and clipboard use veilstrat where the catalogue uses aiagentskills.

npx veilstrat add skill yanko-belov/code-craft --skill immutability

  • SKILL.md5.5 KB

Overview

This skill enforces immutable update patterns for objects and arrays in TypeScript code. It guides developers to never mutate function parameters or shared state and to always return new objects, making changes explicit and predictable. The result is fewer subtle bugs, reliable React/Redux state updates, and easier debugging and undo/redo flows.

How this skill works

It inspects code paths where objects or arrays are modified and flags direct mutations (assignments, push/pop/splice, sort without copying, delete, etc.). It recommends and demonstrates immutable alternatives using spread, filter, map, slice, and deep-copy patterns, and points to helpers like immer for complex nested updates. It emphasizes returning new references so change detection and reasoning remain correct.

When to use it

  • When modifying objects or arrays
  • When updating React or Redux state
  • When a function receives an object or array parameter
  • When data is shared across modules or components
  • When state changes cause intermittent or hard-to-reproduce bugs

Best practices

  • Never mutate inputs; always return new copies.
  • Use spread ({ ...obj }) for shallow updates and map/filter for arrays.
  • For nested updates, copy each level or use a library like immer.
  • Avoid array-mutating methods (push, pop, splice, sort) directly; operate on copied arrays.
  • If concerned about performance, profile first—engines optimize short-lived objects.

Example use cases

  • Updating a user address without changing the original user object.
  • Adding or removing items from a cart with [...cart, item] or cart.filter(...).
  • Changing a theme setting nested inside config by copying each level.
  • Implementing undo/redo by keeping previous immutable states in history.
  • Refactoring code that mutates parameters to return new values to prevent cross-module side effects.

FAQ

Direct mutation may be simpler in the short term but creates hidden coupling and unpredictable bugs. Immutability makes intent explicit, simplifies reasoning, and prevents regressions as code grows.

Won't creating new objects hurt performance?

Modern JS engines are optimized for creating short-lived objects; the cost is usually negligible versus the debugging and correctness cost. Profile only if you see concrete performance issues.

Built by
VeilStrat
AI signals for GTM teams
© 2026 VeilStrat. All rights reserved.All systems operational
immutability skill by yanko-belov/code-craft | VeilStrat