Skip to content

A TSLint rule which forbids setting array properties, because they aren't handled by JSON.stringify.

License

Notifications You must be signed in to change notification settings

leroydev/no-setting-array-properties-rule

Repository files navigation

NPM version Downloads

No setting array properties rule

A TSLint rule which forbids setting array properties, because they aren't handled by JSON.stringify.

Description

When you call JSON.stringify on an array with custom properties, those properties aren't serialized. Example:

let arr: number[] = [1, 2];
const prop2: string = "prop2";

arr["prop1"] = "1";
arr[prop2] = "2";

JSON.stringify(arr); //results in [1,2]

This rule forbids property assignment and access on array types, so it forbids these two statements in the example above:

  • arr["prop1"] = "1";
  • arr[prop2] = "2";

Usage

  • Install it using npm install no-setting-array-properties-rule --save-dev.
  • Add it to your tslint.json, like this:
{
   "rulesDirectory": [
       "node_modules/no-setting-array-properties-rule/dist/src"
   ],
   "rules": {
       "no-setting-array-properties": true
   }
}

Note that this rule doesn't work in vscode-tslint at the moment, because it's a TypedRule. See this vscode-tslint issue for more details.

Changelog

See CHANGELOG.md.

About

A TSLint rule which forbids setting array properties, because they aren't handled by JSON.stringify.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published