Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

tslint-immutable

Package Overview
Dependencies
Maintainers
1
Versions
56
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

tslint-immutable

TSLint rules to disable mutation in TypeScript.

  • 4.1.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
18K
increased by204.74%
Maintainers
1
Weekly downloads
 
Created
Source

tslint-immutable

npm version travis build MIT license

TSLint rules to disable mutation in TypeScript.

Background

In some applications it is important to not mutate any data, for example when using Redux to store state in a React application. Moreover immutable data structures has a lot of advantages in general so I want to use them everywhere in my applications.

I originally used immutablejs for this purpose. It is a really nice library but I found it had some drawbacks. Specifically when debugging it was hard to see the structure, creating JSON was not straightforward, and passing parameters to other libraries required converting to regular mutable arrays and objects. The seamless-immutable project seems to have the same conclusions and they use regular objects and arrays and check for immutability at run-time. This solves all the aformentioned drawbacks but introduces a new drawback of only being enforced at run-time. (Altough you loose the structural sharing feature of immutablejs with this solution so you would have to consider if that is something you need).

Then typescript 2.0 came along and introduced readonly options for properties, indexers and arrays. This enables us to use regular object and arrays and have the immutability enfored at compile time instead of run-time. Now the only drawback is that there is nothing enforcing the use of readonly in typescript.

This can be solved by using linting rules. So the aim of this project is to leverage the type system in typescript to enforce immutability at compile-time while still using regular objects and arrays.

Installing

npm install tslint-immutable --save-dev

See the example tslint.json file for configuration.

Compability

  • tslint-immutable 3.x.x is compatible with tslint 5.x.x.
  • tslint-immutable 2.x.x is compatible with tslint 4.x.x.
  • tslint-immutable 1.x.x is compatible with tslint 3.x.x.

TSLint Rules

In addition to immutable rules this project also contains a few rules for enforcing a functional style of programming and a few other rules. The following rules are available:

Immutability rules

readonly-keyword

This rule enforces use of the readonly modifier. The readonly modifier can appear on property signatures and index signatures.

Below is some information about the readonly modifier and the benefits of using it:

You might think that using const would eliminate mutation from your TypeScript code. Wrong. Turns out that there's a pretty big loophole in const.

interface Point { x: number, y: number }
const point: Point = { x: 23, y: 44 };
point.x = 99; // This is legal

This is why the readonly modifier exists. It prevents you from assigning a value to the result of a member expression.

interface Point { readonly x: number, readonly y: number }
const point: Point = { x: 23, y: 44 };
point.x = 99; // <- No object mutation allowed.

This is just as effective as using Object.freeze() to prevent mutations in your Redux reducers. However the readonly modifier has no run-time cost, and is enforced at compile time. A good alternative to object mutation is to use the ES2016 object spread syntax that was added in typescript 2.1:

interface Point { readonly x: number, readonly y: number }
const point: Point = { x: 23, y: 44 };
const transformedPoint = { ...point, x: 99 };

Note that you can also use object spread when destructuring to delete keys in an object:

let { [action.id]: deletedItem, ...rest } = state;

The readonly modifier also works on indexers:

let foo: { readonly [key:string]: number };
Has Fixer

Yes

Options
Example config
"readonly-keyword": true
"readonly-keyword": [true, "ignore-local"]
"readonly-keyword": [true, "ignore-local", {"ignore-prefix": "mutable"}]

readonly-array

This rule enforces use of ReadonlyArray<T> instead of Array<T> or T[].

Below is some information about the ReadonlyArray<T> type and the benefits of using it:

Even if an array is declared with const it is still possible to mutate the contents of the array.

interface Point { readonly x: number, readonly y: number }
const points: Array<Point> = [{ x: 23, y: 44 }];
points.push({ x: 1, y: 2 }); // This is legal

Using the ReadonlyArray<T> type will stop this mutation:

interface Point { readonly x: number, readonly y: number }
const points: ReadonlyArray<Point> = [{ x: 23, y: 44 }];
points.push({ x: 1, y: 2 }); // Unresolved method push()
Has Fixer

Yes

Options
Example config
"readonly-array": true
"readonly-array": [true, "ignore-local"]
"readonly-array": [true, "ignore-local", {"ignore-prefix": "mutable"}]

no-let

This rule should be combined with tslint's built-in no-var-keyword rule to enforce that all variables are declared as const.

There's no reason to use let in a Redux/React application, because all your state is managed by either Redux or React. Use const instead, and avoid state bugs altogether.

let x = 5; // <- Unexpected let or var, use const.

What about for loops? Loops can be replaced with the Array methods like map, filter, and so on. If you find the built-in JS Array methods lacking, use ramda, or lodash-fp.

const SearchResults =
  ({ results }) =>
    <ul>{
      results.map(result => <li>result</li>) // <- Who needs let?
    }</ul>;

no-object-mutation

This rule prohibits syntax that mutates existing objects via assignment to or deletion of their properties. While requiring the readonly modifier forces declared types to be immutable, it won't stop assignment into or modification of untyped objects or external types declared under different rules. Forbidding forms like a.b = 'c' is one way to plug this hole. Inspired by the no-mutation rule of eslint-plugin-immutable.

const x = {a: 1};

x.foo = 'bar'; // <- Modifying properties of existing object not allowed.
x.a += 1; // <- Modifying properties of existing object not allowed.
delete x.a; // <- Modifying properties of existing object not allowed.

Functional style rules

no-this, no-class

Thanks to libraries like recompose and Redux's React Container components, there's not much reason to build Components using React.createClass or ES6 classes anymore. The no-this rule makes this explicit.

const Message = React.createClass({
  render: function() {
    return <div>{ this.props.message }</div>; // <- no this allowed
  }
})

Instead of creating classes, you should use React 0.14's Stateless Functional Components and save yourself some keystrokes:

const Message = ({message}) => <div>{ message }</div>;

What about lifecycle methods like shouldComponentUpdate? We can use the recompose library to apply these optimizations to your Stateless Functional Components. The recompose library relies on the fact that your Redux state is immutable to efficiently implement shouldComponentUpdate for you.

import { pure, onlyUpdateForKeys } from 'recompose';

const Message = ({message}) => <div>{ message }</div>;

// Optimized version of same component, using shallow comparison of props
// Same effect as React's PureRenderMixin
const OptimizedMessage = pure(Message);

// Even more optimized: only updates if specific prop keys have changed
const HyperOptimizedMessage = onlyUpdateForKeys(['message'], Message);

no-mixed-interface

Mixing functions and data properties in the same interface is a sign of object-orientation style. This rule enforces that an inteface only has one type of members, eg. only data properties or only functions.

no-expression-statement

When you call a function and don’t use it’s return value, chances are high that it is being called for its side effect. e.g.

array.push(1)
alert('Hello world!')

This rule checks that the value of an expression is assigned to a variable and thus helps promote side-effect free (pure) functions.

Options

Using the ignore-local option

If a tree falls in the woods, does it make a sound? If a pure function mutates some local data in order to produce an immutable return value, is that ok?

The quote above is from the clojure docs. In general, it is more important to enforce immutability for state that is passed in and out of functions than for local state used for internal calculations within a function. For example in Redux, the state going in and out of reducers needs to be immutable while the reducer may be allowed to mutate local state in its calculations in order to achieve higher performance. This is what the ignore-local option enables. With this option enabled immutability will be enforced everywhere but in local state. Function parameters are not considered local state so they will still be checked.

Note that using this option can lead to more imperative code in functions so use with care!

Using the ignore-prefix option

Some languages are immutable by default but allows you to explicitly declare mutable variables. For example in reason you can declare mutable record fields like this:

type person = {
  name: string,
  mutable age: int
};

Typescript is not immutable by default but it can be if you use this package. So in order to create an escape hatch similar to how it is done in reason the ignore-prefix option can be used. For example if you configure it to ignore variables with names that has the prefix "mutable" you can emulate the above example in typescript like this:

type person = {
  readonly name: string,
  mutableAge: number // This is OK with ignore-prefix = "mutable"
};

Yes, variable names like mutableAge are ugly, but then again mutation is an ugly business :-).

no-var-keyword

Without this rule, it is still possible to create var variables that are mutable.

typedef with call-signature option

For performance reasons, tslint-immutable does not check implicit return types. So for example this function will return an mutable array but will not be detected (see #18 for more info):

function foo() {
  return [1, 2, 3];
}

To avoid this situation you can enable the built in typedef rule like this:

"typedef": [true, "call-signature"]

Now the above function is forced to declare the return type becomes this and will be detected.

Sample Configuration File

Here's a sample TSLint configuration file (tslint.json) that activates all the rules:

{
  "extends": [
    "tslint-immutable"
  ],
  "rules": {

    // Recommended built-in rules
    "no-var-keyword": true,
    "typedef": [true, "call-signature"],

    // Immutability rules
    "readonly-keyword": true,
    "readonly-array": true,
    "no-let": true,
    "no-object-mutation": true,

    // Functional style rules
    "no-this": true,
    "no-class": true,
    "no-mixed-interface": true,
    "no-expression-statement": true,

  }
}

How to contribute

For new features file an issue. For bugs, file an issue and optionally file a PR with a failing test. Tests are really easy to do, you just have to edit the *.ts.lint files under the test directory. Read more here about tslint testing.

How to develop

To execute the tests run yarn test. To release a new package version run yarn publish:patch, yarn publish:minor, or yarn publish:major.

Prior work

This work was originally inspired by eslint-plugin-immutable.

Keywords

FAQs

Package last updated on 21 Aug 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc