@putout/plugin-types
🐊Putout plugin adds ability to transform code related to types assertions
Last updated a year ago by coderaiser .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ npm install @putout/plugin-types 
SYNC missed versions from official npm registry.

@putout/plugin-types NPM version

🐊Putout plugin adds ability to help with transforming code related to types.

Install

npm i putout @putout/plugin-types -D

Rules

Config

{
    "rules": {
        "types/declare": "on",
        "types/convert-typeof-to-istype": "on",
        "types/remove-useless-conversion": "on",
        "types/remove-double-negations": "on",
        "types/remove-useless-typeof": "on",
        "types/apply-is-array": "on"
    }
}

declare

Based on @putout/operator-declare. Supported assertions:

  • isString;
  • isEmptyString;
  • isNumber;
  • isFn;
  • isBool;
  • isObject;
  • isUndefined;
  • isSymbol;
  • isNull;
  • isBigInt;
  • isArray;
  • isEmptyArray;

❌ Example of incorrect code

isString('hello');

βœ… Example of correct code

const isString = (a) => typeof a === 'string';
isString('hello');

When you want to skip some declaration use dismiss:

{
    "rules": {
        "types/declare": ["on", {
            "dismiss": ["isString"]
        }]
    }
}

convert-typeof-to-is-type

The typeof operator returns a string indicating the type of the unevaluated operand.

(c) MDN

❌ Example of incorrect code

if (typeof a === 'boolean')
    return x;

βœ… Example of correct code

const isBool = (a) => typeof a === 'boolean';

if (isBool(a))
    return x;

remove-useless-conversion

❌ Example of incorrect code

const a = !![1].includes(1);
const b = Boolean([1].includes(1));

βœ… Example of correct code

const a = [1].includes(1);

remove-double-negations

It is possible to use a couple of NOT operators (!!) in series to explicitly force the conversion of any value to the corresponding boolean primitive. The conversion is based on the "truthyness" or "falsyness" of the value.

The same conversion can be done through the Boolean function.

(c) MDN

❌ Example of incorrect code

if (!!a)
    console.log('hi');

βœ… Example of correct code

if (a)
    console.log('hi');

remove-useless-typeof

The typeof operator returns a string indicating the type of the unevaluated operand.

(c) MDN

❌ Example of incorrect code

typeof typeof 'hello';

βœ… Example of correct code

typeof 'hello';

apply-is-array

The Array.isArray() method determines whether the passed value is an Array. When checking for Array instance, Array.isArray() is preferred over instanceof because it works through iframes.

❌ Example of incorrect code

x instanceof Array;

βœ… Example of correct code

const {isArray} = Array;
isArray(x);

In case of using inline option:

{
    "rules": {
        "apply-is-array": ["on", {
            "inline": true
        }]
    }
}

Array.isArray will be inlined:

Array.isArray(x);

License

MIT

Comparison

Linter Rule Fix
🐊 Putout types βœ…
⏣ ESLint no-implicit-coercion βœ…

License

MIT

Current Tags

  • 4.1.0                                ...           latest (6 months ago)

18 Versions

  • 4.1.0                                ...           6 months ago
  • 4.0.0                                ...           7 months ago
  • 3.0.0                                ...           a year ago
  • 2.6.0                                ...           a year ago
  • 2.5.0                                ...           a year ago
  • 2.4.0                                ...           a year ago
  • 2.3.0                                ...           a year ago
  • 2.2.0                                ...           a year ago
  • 2.1.0                                ...           a year ago
  • 2.0.0                                ...           a year ago
  • 1.7.0                                ...           a year ago
  • 1.6.0                                ...           a year ago
  • 1.5.0                                ...           2 years ago
  • 1.4.0                                ...           2 years ago
  • 1.3.0                                ...           2 years ago
  • 1.2.0                                ...           2 years ago
  • 1.1.0                                ...           2 years ago
  • 1.0.0                                ...           2 years ago
Maintainers (1)
Downloads
Total 0
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (0)
None
Dev Dependencies (10)
Dependents (1)

© 2010 - cnpmjs.org x YWFE | Home | YWFE